Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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文件并打印差异_Python_Python 3.x_Csv - Fatal编程技术网

Python 比较两个CSV文件并打印差异

Python 比较两个CSV文件并打印差异,python,python-3.x,csv,Python,Python 3.x,Csv,我试图比较CSV文件,我有两个CSV文件-CSVFileOne,CSVFileTwo。 我想要的输出是打印CSVFileOne,但只打印CSVFileTwo中不存在的行 我的代码: input_file = CSVFileOne ABGSOne = [] with open(input_file, encoding='UTF-8') as fone: rowsOne = csv.reader(fone,delimiter=",",lineterminator="\n") next

我试图比较CSV文件,我有两个CSV文件-CSVFileOne,CSVFileTwo。 我想要的输出是打印CSVFileOne,但只打印CSVFileTwo中不存在的行

我的代码:

input_file = CSVFileOne
ABGSOne = []
with open(input_file, encoding='UTF-8') as fone:
    rowsOne = csv.reader(fone,delimiter=",",lineterminator="\n")
    next(rowsOne, None)
    for rowOne in rowsOne:
        abbgone = {}
        abbgone['astringOne'] = row[0]
        abbgone['bstringOne'] = row[1]
        abbgone['cstringOne'] = row[2]
        abbgone['dstringOne'] = row[3]
        ABGSOne.append(abbgone)



input_fileTwo = CSVFileTwo
ABGSTwo = []
with open(input_fileTwo, encoding='UTF-8') as ftwo:
    rowsTwo = csv.reader(ftwo,delimiter=",",lineterminator="\n")
    next(rowsTwo, None)
    for rowTwo in rowsTwo:
        abbgtwo = {}
        abbgtwo['astringTwo'] = row[0]
        abbgtwo['bstringTwo'] = row[1]
        abbgtwo['cstringTwo'] = row[2]
        abbgtwo['dstringTwo'] = row[3]
        ABGSOne.append(abbgTwo)



for abbgone in ABGSOne:
   if abbgone['bstringOne'] == abbgtwo['bstringTwo']:
        print('abbgone['bstringOne']
试试这个

with open('CSVFileOne.csv', 'r') as t1, open('CSVFileTwo.csv', 'r') as t2:
fileone = t1.readlines()
filetwo = t2.readlines()

with open('Desired.csv', 'w') as outFile:
for line in filetwo:
    if line not in fileone:
        outFile.write(line)
这可能会有帮助。