Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用csv.DictReader逐行比较两个.csv文件中的两列_Python_Csv - Fatal编程技术网

Python 使用csv.DictReader逐行比较两个.csv文件中的两列

Python 使用csv.DictReader逐行比较两个.csv文件中的两列,python,csv,Python,Csv,我有两个大的.csv文件,我想在其中使用csv DictReader或甚至pandas逐行比较两列 我需要检查两个文件中特定列的所有行是否相同。我在这里看到了一些建议,但没有一个在我的情况下起作用。问题是对第二个打开的文件的迭代顺序不正确,即使这些文件是相同的 我已经使用openpyxl快速完成了搜索和修改任务,但是由于csv文件大小为数百MB,即使在运行时也将csv转换为excel似乎不是一个好的决定 以下是我目前掌握的代码: import csv class CsvCompareTeste

我有两个大的.csv文件,我想在其中使用
csv DictReader
或甚至
pandas
逐行比较两列

我需要检查两个文件中特定列的所有行是否相同。我在这里看到了一些建议,但没有一个在我的情况下起作用。问题是对第二个打开的文件的迭代顺序不正确,即使这些文件是相同的

我已经使用
openpyxl
快速完成了搜索和修改任务,但是由于csv文件大小为数百MB,即使在运行时也将csv转换为excel似乎不是一个好的决定

以下是我目前掌握的代码:

import csv

class CsvCompareTester:

    work_csv_path = None
    test_csv_path = None

    @staticmethod
    def insert_file_paths():
        print()
        print('Enter the full absolute path of the WORK .csv file:')
        CsvCompareTester.work_csv_path = input()

        print('Enter the full absolute path of the TEST .csv file:')
        CsvCompareTester.test_csv_path = input()

    @staticmethod
    def compare_files(work_csv_file, test_csv_file):

        work_csv_obj = csv.DictReader(work_csv_file, delimiter=";")
        test_csv_obj = csv.DictReader(test_csv_file, delimiter=";")

        for work_row in work_csv_obj:
            for test_row in test_csv_obj:
                if work_row == test_row:
                    print('ALL CLEAR')
                    print(str(work_row))
                    print(str(test_row))
                    print()
                else:
                    print("STRINGS DON'T MATCH")
                    print(str(work_row))
                    print(str(test_row))
                    print()


if __name__ == "__main__":
    csv_tester = CsvCompareTester()
    csv_tester.insert_file_paths()

    with open(CsvCompareTester.work_csv_path) as work_file:
        with open(CsvCompareTester.test_csv_path) as test_file:
            csv_tester.compare_files(work_file, test_file)
如何迭代.csv文件行,同时还能按键或值处理特定的行和列(这肯定会减少无用的迭代次数)。 出于某种原因,在上面的代码中,第一个文件中的每一行字符串都与第二个文件中的另一行字符串不匹配。文件是相同的,并且具有相同的条目顺序,我已经仔细检查过了。
为什么第二个文件不作为第一个文件从开始到结束进行迭代?

问题在于如何在文件上循环。按照您的方式,尝试将第一个文件的每一行与第二个文件的每一行进行比较。相反,您需要在锁定步骤中获取它们的行,一个很好的方法是使用内置函数

因此,请改为:

    @staticmethod
    def compare_files(work_csv_file, test_csv_file):

        work_csv_obj = csv.DictReader(work_csv_file, delimiter=";")
        test_csv_obj = csv.DictReader(test_csv_file, delimiter=";")

#        for work_row in work_csv_obj:
#            for test_row in test_csv_obj:

        for work_row, test_row in zip(work_csv_obj, test_csv_obj):
            if work_row == test_row:
                print('ALL CLEAR')
                print(str(work_row))
                print(str(test_row))
                print()
            else:
                print("STRINGS DON'T MATCH")
                print(str(work_row))
                print(str(test_row))
                print()
顺便说一句,尽管它可能还没有引起任何问题,但我也注意到您没有按照文档中所示正确打开这两个文件-您遗漏了
newline=''
参数

以下是正确的方法:

if __name__ == "__main__":
    csv_tester = CsvCompareTester()
    csv_tester.insert_file_paths()

#    with open(CsvCompareTester.work_csv_path) as work_file:
#        with open(CsvCompareTester.test_csv_path) as test_file:

    with open(CsvCompareTester.work_csv_path, newline='') as work_file:
        with open(CsvCompareTester.test_csv_path, newline='') as test_file:
            csv_tester.compare_files(work_file, test_file)