Python 比较两个CSV文件并打印差异
我试图比较CSV文件,我有两个CSV文件-CSVFileOne,CSVFileTwo。 我想要的输出是打印CSVFileOne,但只打印CSVFileTwo中不存在的行 我的代码: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
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)
这可能会有帮助。