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. The problem is that it is evaluating the loop only once.
File1:
ABC
DEF
JKL
File2:
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:
ABC 456 HJK
Please help me on this.
You need to add
fh_file2.seek(0)before the secondforloop to start over at the beginning of the file.You’d be better served, however, by reading it into memory once:
then iterating over
file2_lines. Reading the file from disk for each line in another file is going to be very slow.