python - 使用python读取txt文件并回答问题

标签 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 

所以我将这个数据列表作为 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/

相关文章:

python - 最短路径搜索 - 使用边类型 [NetworkX、igraph]

python - ctypes - 将带有指针的结构传递给另一个结构

python - 用于多类分类的 PyTorch LSTM : TypeError: '<' not supported between instances of 'Example' and 'Example'

python - 为什么得到意想不到的字典

python - 如何将列表中元组中的整数与字典中另一个列表中另一个元组中的另一个整数进行比较?

python - 如何将字典以某种格式写入文件

python - python 中的输出结果不正确 : Text extraction for . pdf、.pptx 和 .docx

python - 使用 matplotlib 将 RGBA 元组转换为十六进制

python - VMWare pyvmomi 6.0.0 : Operation not supported

python - 我怎样才能杀死一个 cron 程序?