简体   繁体   English

如果嵌套``for''循环中的语句仅评估一次-python

[英]If statement inside nested 'for' loops is evaluating only once - python

I am writing a code which compares file1 (single column of entries) with file 2 (3 column of entries) and fetch matched records from file 2 on basis of first column. 我正在编写一个代码,将文件1(条目的单列)与文件2(条目的3列)进行比较,并根据第一列从文件2中获取匹配的记录。 The problem is that it is evaluating the loop only once. 问题是它只评估一次循环。

File1: 文件1:

ABC

DEF

JKL

File2: 文件2:

IJK,123,SDF

ABC,456,HJK

QWE,876,GFT

JKL,098,HGF

..... .....

My code: 我的代码:

for entry in fh_file1:
    mir = entry.strip('\n')
    print(mir)
    for row in fh_file2:
        row_splt = row.split(',')             
        print(row_splt[0])
        if mir in row_splt[0]:
            print (row.strip('\n'))
        else:
            pass

Result from that code: 该代码的结果:

is just the match of first entry of file 1: 只是文件1的第一个条目的匹配项:

ABC 456 HJK

Please help me on this. 请帮我。

Files are streams of data. 文件是数据流。 When you loop over them, you read them a line at a time. 遍历它们时,您一次阅读一行。 At the end of the inner loop, that file has reached the end. 在内部循环的末尾,该文件已到达末尾。 It will not start again at the beginning for the next iteration of the outer loop, because that's not how files work. 它不会在外循环的下一次迭代的开始处再次开始,因为这不是文件的工作方式。

You should usually read the file into memory first: list(fh_file1) will give you a list of lines that you can loop over as many times as you like. 通常,您应该首先将文件读入内存: list(fh_file1)将为您提供行的list ,您可以根据需要循环多次。

You need to add fh_file2.seek(0) before the second for loop to start over at the beginning of the file. 您需要在第二个for循环之前添加fh_file2.seek(0)才能从文件开头开始。

You'd be better served, however, by reading it into memory once: 但是,最好将其读入内存一次,这样会更好:

file2_lines = fh.file2.readlines()

then iterating over file2_lines . 然后遍历file2_lines Reading the file from disk for each line in another file is going to be very slow. 从磁盘读取另一个文件中每一行的文件将非常缓慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM