开发者

for loop problem [duplicate]

开发者 https://www.devze.com 2023-03-03 00:43 出处:网络
This question already has an answer here: Script skips second for loop when reading a file (1 answer) Closed 7 years ago.
This question already has an answer here: Script skips second for loop when reading a file (1 answer) Closed 7 years ago.

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]:
            开发者_如何学Gocounter += 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.


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

in2.seek(0)

before the inner loop.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消