【问题标题】:To store Datetime type in csv file将日期时间类型存储在 csv 文件中
【发布时间】:2020-04-03 15:37:40
【问题描述】:

我想将日期时间存储在 CSV 文件中。 我尝试了以下代码

date=location.iloc[0,1]
lastdate=location.iloc[len(location)-1,1]
import csv
f = open('numbers3.csv', 'w')

with f:

    writer = csv.writer(f)
    writer.writerow(fields)
    while(date==lastdate) or (date<lastdate):
        print(date)
        strr=str(date)
        writer.writerow(str(date))
        date=date+timedelta(minutes=15)

输出是

我想要以下输出

【问题讨论】:

  • 请删除屏幕截图并添加文字。

标签: python csv dataframe


【解决方案1】:

如果要将datetime 的列表存储到csv 文件的列中,则需要将所有datetime 元素以这种格式放入一个列表中:

datetimes = [['2020-10-10 13:12'], ['2020-10-10 13:12'], ['2020-10-10 13:12'], ['2020-10-10 13:12']]
myFile = open('/path/csvexample3.csv', 'w')
with myFile:
    writer = csv.writer(myFile)
    writer.writerows(datetimes)

那你就可以走了。请注意,每个日期都在list 中,而datetimeslistlist

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2017-08-29
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多