1

i have this code

animal_names, dates, locations = [], [], []

filename=input("Enter name of file:")
if filename=="animallog1.txt":
    data=open('animallog1.txt','r')
    information=data.read()
    for line in information:
        animal_name, date, location = line.strip().split(':')
        animal_names.append(animal_name)
        dates.append(date)
        locations.append(location)   

    print(animal_names)
    print(dates)
    print(location)

i am trying to use the data in the txt file to print me the results i want the txt file contains the following :

a01:01-24-2011:s1 
a03:01-24-2011:s2 
a02:01-24-2011:s2 
a03:02-02-2011:s2 
a03:03-02-2011:s1 
a02:04-19-2011:s2 
a01:05-14-2011:s2 
a02:06-11-2011:s2 
a03:07-12-2011:s1 
a01:08-19-2011:s1 
a03:09-19-2011:s1 
a03:10-19-2011:s2 
a03:11-19-2011:s1 
a03:12-19-2011:s2  

which is in the format animal_name:date:location

using the above i want to get

animal_names=[a01,a02, #till the very end,a03]

same for the rest of them(date,location), how can i fix my code so that is my result

I also need to use these lists the answer questions later

4
  • What do you get for output when you run your code? Commented Nov 25, 2013 at 0:38
  • Why don't you make an Animal class that encapsulates all that data in one place rather than keeping it in three separate lists? Commented Nov 25, 2013 at 0:39
  • builtins.ValueError: need more than 1 value to unpack @duhaime Commented Nov 25, 2013 at 0:41
  • print(location) should be print(locations), notice the plural s? And the above the error shows you haven't pass in arguments. Commented Nov 25, 2013 at 0:42

1 Answer 1

3

Or

def main():
    fname = input("Enter name of file: ")
    with open(fname) as inf:
        names, dates, locations = zip(*[line.strip().split(':') for line in inf])

    print(names)
    print(dates)
    print(locations)

if __name__=="__main__":
    main()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.