本文介绍了for循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
For循环问题:
For loop issue:
in1 = open('file_1', 'r')
in2 = open('file_2', 'r')
outf = open('out_file', 'w')
for line in in1:
s = line.split('\t')
A = s[1][:-1]
B = s[0]
counter = 0
for line in in2:
ss = line.split('\t')
if A == ss[0] or A == ss[1]:
counter += 1
outf.write('%s\t%s\t%s\n'%(A,B,counter))
问题在于它只是通过来处理in2中的行:
对于in1中的第一个行
。我似乎无法找出原因。
The problem is that it is only going through for line in in2:
for the first line in in1
. I can't seem to figure out why.
推荐答案
您只能迭代一次文件。再次从头开始,使用
You can iterate over a file only once. To start from the beginning again, use
in2.seek(0)
在内部循环之前。
这篇关于for循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!