python - Compare csv files rank order -


i trying compare first 300 lines of 1 csv file another. iterating through each line of first file. within iteration iterating through second file, counting each iteration, until find match. outputing counting value (row of matched line in second file) text file.

here have far:

    import csv  open('/volumes/cachannel/cutler/subsampling/in-silico_idc18_gfp18_names_only.csv', 'rb') file1:     file1reader = csv.reader(file1)     open('/volumes/cachannel/cutler/subsampling/icd18_gfp18_names_only.csv', 'rb') file2:         file2reader = csv.reader(file2)          header1 = file1reader.next()         header2 = file2reader.next()          count = 0          open("rank_results.txt", 'a') outfile:             while count < 300:                 print(count)                 line1 in file1reader:                     linenum = 1                     line2 in file2reader:                         if line1 == line2:                             print('match found')                             outfile.write(str(linenum))                         else:                             linenum += 1                 count += 1 

the error getting finds first match , doesn't find others - when know there more.

just clear here example:

file 1:

bob sue  sally herald 

file 2:

sue  bob  herald  sally 

output file:

2 1 4 3 

your primary problem trying iterate on second file multiple times. in order iterate on entire contents more once, need close , reopen it. can let python count line numbers automatically using enumerate() , count number of matches storing them in list , checking length.

another problem sample input files don't have header lines in them. assuming contain them, following should want:

import csv  max_count = 300 filename1 = '/volumes/cachannel/cutler/subsampling/in-silico_idc18_gfp18_names_only.csv' filename2 = '/volumes/cachannel/cutler/subsampling/icd18_gfp18_names_only.csv' matches = []  open(filename1, 'rb') file1:     file1reader = csv.reader(file1)     header1 = file1reader.next()      line1 in file1reader:         open(filename2, 'rb') file2:             file2reader = csv.reader(file2)             header2 = file2reader.next()              linenum, line2 in enumerate(file2reader, start=1):                 if line1 == line2:                     print('match found')                     matches.append(str(linenum))                     if len(matches) >= max_count:                         break          if len(matches) >= max_count:             break  open("rank_results.txt", 'w') outfile:     outfile.write(' '.join(matches) + '\n')