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
所以我将这个数据列表作为 txt 文件,其中动物名称:日期:位置
所以我必须阅读这个 txt 文件来回答问题。
到目前为止我已经
text_file=open("animal data.txt", "r") #open the text file and reads it.
我知道如何阅读一行,但由于有多行,我不确定如何阅读 txt 中的每一行。
最佳答案
使用 for
循环。
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()
关于python - 使用python读取txt文件并回答问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169159/