for loop problem [duplicate]

99封情书 提交于 2020-01-11 13:14:08

问题


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))

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.


回答1:


You can iterate over a file only once. To start from the beginning again, use

in2.seek(0)

before the inner loop.




回答2:


The first time you loop over in2, you consume it. Either reopen it, or seek back to the beginning.




回答3:


Once you have read each line from file_2 in the inner loop then in2 is at end-of-file. If you want to read file_2 for each line in file_1 then add:

    in2.seek(0)

just before or after the write.




回答4:


When working with files, please do this

with open('out_file', 'w') as outf:
    with open('file_1', 'r') as in1:
        for line in in1:
            s = line.split('\t')
            a = s[1][:-1]
            b = s[0]
            counter = 0
            with open('file_2', 'r') as in2:
                for line in in2:
                    etc.

Using with assures your files are closed.

Opening a file in the smallest enclosing scope guarantees it can be read all the way through. It's costly to keep reopening the file, but there are a lot of ways to speed up this application.

Also, please use only lowercase variable names. Reserve Uppercase for class names.



来源:https://stackoverflow.com/questions/5886193/for-loop-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!