简体   繁体   English

Python:比较CSV文件并保存与第一行的区别(列名)

[英]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 文件1June_01_2018.csv

在此处输入图片说明

File 2 : June_02_2018.csv 文件2June_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.

相关问题 Python:加载CSV,第一列作为行名,第一行作为列名 - Python: Load CSV, first column as row names, first row as column names 如何按列比较两个CSV文件并使用Pandas Python将CSV文件中的差异保存 - How to compare two CSV files by column and save the differences in csv file using pandas python Python:比较文件的第一行并保存其路径 - Python: Compare first line of files and save their path 比较两个 CSV 文件并在同一个文件中写入差异作为 python 中的额外列 - Compare two CSV files and write difference in the same file as an extra column in python 如何在 python 中生成包含多个 csv 文件的最后一行和第一行的差异的报告? - How to generate a report in python containing the difference of last and first row of multiple csv files? 导入 csv:从第一行的列名中删除文件名 - Import csv: remove filename from column names in first row 使用 python 比较 2 csv 文件 pandas 并将 output 保存在列表或集合中 - Compare 2 csv files using python pandas and save the output in a list or set Python-如何使用不同的文件名保存CSV文件? - Python - How to save CSV files with different file names? 有没有function可以在python中保存多个csv不同名字的文件? - Is there a function to save multiple csv files with different names in python? 如何在Python中将列的第一个元素与CSV文件中的其他元素进行比较? - How to compare first element of a column with other elements in CSV file in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM