简体   繁体   English

如何读取文件,然后从该文件中获取每个元素,然后搜索另一个文件以查看其是否包含该元素。 蟒蛇。

[英]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. 我正在尝试编写一段代码,将在python中打开一个CSV文件,并解析每一行以及每一行中的每个元素。 Then see if each element is in another CSV file, if it is write it to a third file. 然后查看每个元素是否在另一个CSV文件中,如果已将其写入第三个文件。 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: . 您的问题是if item in file2: You open file2 , but you don't process it. 打开file2 ,但不对其进行处理。 in isn't going to implement the search for you. in是不会实现你的搜索。 You'll need at least load file2 before searching in it for item . 你需要在它的搜索之前至少加载文件2 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]))

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

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