简体   繁体   中英

How do I read in a file, then take each element from this file and search another file to see if it contains the element. Python.

I am trying to write a piece of code that will open a CSV file, in python, and parse through each line and each element in each line. Then see if each element is in another CSV file, if it is write it to a third file. This is the code I have at present, through testing I have determined that my searching algorithm is what is not working correctly...

import csv

def generateKnownReport(mypath, GKR):
    if GKR==True:
        report = open("KnownReport.txt", "w")
        file2=frozenset(open("file","r"))
        for row in csv.reader(open("file","r"),delimiter=','):
            for item in row:
                if item in file2:
                    ##report.write(str(row))
                    print('True')
                    break
                else:
                    print('ERROR')
        report.close() 
    else:
        report = open("KnownReport.txt", "w")
        report.write("No Known Report Generated.")
        report.close()

Any help at all is appreciated. Thanks!

Your problem is if item in file2: . You open file2 , but you don't process it. in isn't going to implement the search for you. You'll need at least load file2 before searching in it for item .

The only reasonable way to do this is to read both files into a list or other iterable and then step through it to find differences.

If duplicates are not important, a set will give better performance.

Here is a way to get started:

with open('file-one.csv') as f:
    reader = csv.reader(f, delimiter=',')  # adjust accordingly
    file_one = list(reader)
with open('file-two.csv') as f:
    reader = csv.reader(f, delimiter=',')
    file_two = list(reader)

element_to_search = 0  # 0 = first column in the row
line_pairs = zip(file_one, file_two)

with open('file-three.csv','w') as f:
    for line in line_pairs:
        if line[0][element_to_search] == line[1][element_to_search]:
            f.write('{}\n'.format(line[0]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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