我有CSV文件,其中包含有日期时间的一些信息。我提取日期时间并将其转换为如下日期:
daily_readings={"Date":[]}
TIME = df.iloc[4,4]
report_date = pd.to_datetime(TIME).date()
if report_date not in daily_readings['Date']:
daily_readings['Date'].append(report_date)
我同时导入了datetime和time库(不确定是否确实需要两者)。当我打印字典print(daily_readings['Date'])
时,我得到了这样的输出: datetime.date(2021,12,7),datetime.date(2021,12,8),尽管当我打印变量report_date:print(report_date)
时,我得到了这样一个干净的日期: 2021-12-08。那么,我怎样才能让字典的日期像所显示的那样干净呢?
发布于 2022-01-23 01:10:48
打印datetime.date对象的列表,所看到的是列表元素的表示形式。不要打印整个列表,而是从list元素构造一个str:
import datetime
spam = [datetime.date(2021, 12, 7), datetime.date(2021, 12, 8)]
print(spam)
print(', '.join(map(str, spam)))
print(', '.join(str(item) for item in spam))
print(', '.join(item.strftime('%d.%m.%Y') for item in spam))
输出
[datetime.date(2021, 12, 7), datetime.date(2021, 12, 8)]
2021-12-07, 2021-12-08
2021-12-07, 2021-12-08
07.12.2021, 08.12.2021
或者遍历列表并逐个打印元素。
https://stackoverflow.com/questions/70820478
复制相似问题