[英]Python : Compare CSV files and save the difference with first row(Column Names)
I have two CSV files as shown below: 我有两个CSV文件,如下所示:
File 1 : June_01_2018.csv
文件1 : June_01_2018.csv
File 2 : June_02_2018.csv
文件2 : June_02_2018.csv
Note : I want to find the difference between these two file and store it into the third file with the column headers . 注意 :我想找到这两个文件之间的区别,并将其与列标题一起存储到第三个文件中。
My try : 我的尝试 :
with open('June_01_2018.csv', 'r') as f1:
file1 = f1.readlines()
with open('June_02_2018.csv', 'r') as f2:
file2 = f2.readlines()
with open('June_Updates.csv', 'w') as outFile:
for line in file2:
if line not in file1:
outFile.write(line)
But unable to store the column headers into the third file. 但是无法将列标题存储到第三个文件中。
try this: 尝试这个:
with open('June_01_2018.csv', 'r') as f1:
file1 = f1.readlines()
with open('June_02_2018.csv', 'r') as f2:
file2 = f2.readlines()
with open('June_Updates.csv', 'w') as outFile:
outFile.write(file1[0])
for line in file2:
if line not in file1:
outFile.write(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.