繁体   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.

我正在尝试编写一段代码,将在python中打开一个CSV文件,并解析每一行以及每一行中的每个元素。 然后查看每个元素是否在另一个CSV文件中,如果已将其写入第三个文件。 这是我目前拥有的代码,通过测试,我确定我的搜索算法无法正常工作...

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()

任何帮助都表示赞赏。 谢谢!

您的问题是if item in file2: 打开file2 ,但不对其进行处理。 in是不会实现你的搜索。 你需要在它的搜索之前至少加载文件2 item

唯一合理的方法是将两个文件读入列表或其他可迭代的文件,然后逐步查找差异。

如果重复不重要,则一组将提供更好的性能。

这是一种入门方法:

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