简体   繁体   English

如何比较两个不同 CSV 的日期/日期格式列然后打印出第三个 csv

[英]How to compare the date / date format column of two different CSVs then print out 3rd csv

I need to write a python script that outputs the differences of two csvs into a third csv based on the specific date format, the third csv will hold the differences that are between the two files我需要编写一个 python 脚本,根据特定的日期格式将两个 csv 的差异输出到第三个 csv 中,第三个 csv 将保存两个文件之间的差异

#reads both files and puts them into a table 
Id = "ID"

Date = "Date"

with open('example.csv', 'r') as t1, open ('example2.csv', 'r') as t2:

t1.write(Id + Date "\n")
t1.close()

t2.write(Id + Date "\n")
t2.close()

fileone = t1.readlines() 
filetwo = t2.readlines()

#function to write a third file that outputs differences    

with open ('DIFF.csv', 'w') as outfile:

 for line in filetwo:
    
    if line not in fileone:
       
      #wr = csv.writer(outfile, dialect='csv')
        
      #wr.writerow([line.rstrip('\n')])
        
      outfile.write(line)
 
  outfile.close()

print("csv is ready")

If I got this question right, you have 2 files with date in a particular format listed like this (I'll use my local format, but you can specify the format in the code) :如果我的这个问题是正确的,那么您有 2 个文件,其日期以特定格式列出,如下所示(我将使用我的本地格式,但您可以在代码中指定格式):

example.csv
20/07/2022 15:01
20/07/2022 15:02
20/07/2022 15:03

And:和:

example2.csv
20/07/2022 14:02
20/07/2022 15:01
20/07/2022 15:08

You want to retreive the symmetric difference (date that are on one file but not on the other one) of these files in term of date :您想根据 date 检索这些文件的对称差异(在一个文件上但不在另一个文件上的日期):

output
20/07/2022 15:03
20/07/2022 15:08
20/07/2022 14:02
20/07/2022 15:02

To do so here's the code :为此,代码如下:

from datetime import datetime

#write the format you desire
my_format = "%d/%m/%Y %H:%M\n"

#function that apply to each line to transform the str to a datetime object
str_to_datetime = lambda line: datetime.strptime(line, my_format)

with open('example.csv', 'r') as t1, open ('example2.csv', 'r') as t2, open ('DIFF.csv', 'w') as outfile:
    first_set, second_set = set(map(str_to_datetime, t1.readlines())), set(map(str_to_datetime, t2.readlines()))
    for date in first_set ^ second_set:
        outfile.write(date.strftime(my_format))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个日期列 - 检查它们是否在范围内 - 从第三列取值 - compare two date columns - check if they fall in range - take value from 3rd column python 比较两个不同格式的日期字符串 - python compare two date strings with different format 如何使单列中的两种不同日期格式唯一? - How to make two different date format in single column unique? 如何比较 csv 的 2 个不同列中的 2 个日期,以判断第 1 列中的日期是否在第 2 列之前 - How to compare 2 dates in 2 different columns of a csv to tell if the date in column 1 comes before column 2 更改CSV中日期列的格式 - Change format of date column in CSV 如何比较两列并获取 python pandas dataframe 中两列中所有匹配项的第三列的平均值? - how to compare two columns and get the mean value of the the 3rd column for all matching items in the two in python pandas dataframe? 使用Python 2.7比较2个csv文件并将不同的行输出到第3个CSV文件 - Compare 2 csv files and output different rows to a 3rd CSV file using Python 2.7 如何从文本文件中切出第二列和第三列? 蟒蛇 - How to cut 2nd and 3rd column out of a textfile? python python根据日期打印出csv数据 - python print out csv data based on date 从csv列打印最旧和最新的日期 - 按日期排序csv - Print the the oldest and newest date from a csv column - Sort csv by date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM