【发布时间】:2016-02-22 17:14:29
【问题描述】:
我搜索了 SO 以找到带有特定字符串的输入 txt 文件的第 # 行。 How do I read the first line of a string? 但是我没有得到任何输出,它是空的。我使用以下python代码:
with open(input_f) as input_data:
print input_f # test if reading correct file: yes
for line in input_f: # originally 'in input_data': no output
if line.split('\t', 1)[0] == 'ABC': # string before tab
#if line.startswith('ABC'): ... also empty output
print line # nothing is printed
感谢您的帮助。
【问题讨论】:
-
你需要遍历 input_data,而不是 input_f。
-
向我们展示输入文件。
-
您正在迭代 input_f,它是文件的路径,而不是 input_data,它是文件对象
-
你应该详细说明,不清楚你在问什么。