Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python to read txt files and answer questions

Tags:

python

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 

So I have this list of data as a txt file, where animal name : date : location So I have to read this txt file to answer questions.

So so far I have

text_file=open("animal data.txt", "r") #open the text file and reads it. 

I know how to read one line, but here since there are multiple lines im not sure how i can read every line in the txt.

like image 314
rggod Avatar asked Jan 26 '26 02:01

rggod


1 Answers

Use a for loop.

text_file = open("animal data.txt","r")
for line in text_file:
    line = line.split(":")
    #Code for what you want to do with each element in the line
text_file.close()