简体   繁体   English

为什么日期在 csv 文件中写为浮点数

[英]Why is date being written as float in csv file

I am trying to save some data in csv file.我正在尝试将一些数据保存在 csv 文件中。 Some of the date formats are changed to float.某些日期格式更改为浮动。

This is data:这是数据:

results = [{'Event ID': 15, 'Time Start': '2022/05/04 17:75', 'Time End': '2022/05/04 18:00''}
{'Event ID': 15, 'Time Start': '2022/05/04 18:00', 'Time End': '2022/05/04 20:50'}
{'Event ID': 0, 'Time Start': '2022/05/06 16:50', 'Time End': '2022/05/06 17:00'}
{'Event ID': 4, 'Time Start': '2022/05/09 15:00', 'Time End': '2022/05/09 15:50'}
{'Event ID': 14, 'Time Start': '2022/06/13 07:75', 'Time End': '2022/06/13 08:00'}
{'Event ID': 4, 'Time Start': '2022/06/15 09:00', 'Time End': '2022/06/15 10:50'}
{'Event ID': 14, 'Time Start': '2022/06/16 02:75', 'Time End': '2022/06/16 03:00'}]

The code to save it as csv is as shown below保存为csv的代码如下图

 csv_columns = ['Event ID', 'Time Start', 'Time End']
    csv_file = "ets_results.csv"
    try:
        with open(csv_file, 'w', newline='') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
            writer.writeheader()
            for data in results: 
                print(data)
                writer.writerow(data)
    except IOError:
        print("I/O error") 

The output of the csv file looks like this csv 文件的 output 看起来像这样

在此处输入图像描述

This is happening because of the cell format in excel发生这种情况是因为 excel 中的单元格格式

图像1 When the cell format is 'General' it will show just the number 44728.13542当单元格格式为“常规”时,它将仅显示数字 44728.13542

But when you change it to 'date' you will get what you need.但是,当您将其更改为“日期”时,您将获得所需的内容。 在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM