在下面的代码片段中,我使用一个循环来在元组列表中追加缺少的小时。
hours = []
for i in range(24):
hours.append(dt.time(i, 0))
for an hour in hours: # this loop prints in the str format "HH:MM: SS"
print(hour)
for hour in hours:
check = True
for row in rows:
if row[0] == hour:
check = False
break
if check == True:
rows.append((hour, None, None))
for row in rows: # This loop prints datetime.time(H,0)
print(row)问题是当打印DateTime时。元组中的time对象(第二个循环)输出是:
datetime.time(H,0)
但是,当datetime.time位于列表(第一个循环)中时,它以正确的格式打印:
"HH:MM: SS“
如何插入日期时间。第二种格式的时间是元组吗?
发布于 2021-05-21 16:50:57
您所看到的是str和repr在Python中的区别。第一个循环是将datetime对象打印为str。第二个循环输出为repr,这是Python中的一种字符串表示形式,主要用于调试。
可以使用str()函数强制datetime对象以字符串形式打印,如下所示:
for tup in rows:
print(str(tup[0]), tup[1], tup[2])https://stackoverflow.com/questions/67640678
复制相似问题