这段代码:
dateCol = []
for row in ws3.iter_rows(min_col=1, max_col=1, min_row=7, max_row = None):
for cell in row:
dateCol.append((ws3.cell(row=cell.row, column=1).value))
dateCol
产生这个列表:
[datetime.datetime(2019, 10, 7, 0, 0),
datetime.datetime(2019, 10, 3, 0, 0),
datetime.datetime(2019, 10, 3, 0, 0),
datetime.datetime(2019, 10, 14, 0, 0),
datetime.datetime(2019, 10, 15, 0, 0),
...]
在 Excel 中,日期如下所示:
如何将这些日期转换为 mm/dd/yyyy?如图所示,我一直在努力解决这个问题(我可能会尽快删除它,因为它不太明确):
最佳答案
尝试以下操作:
import datetime
dateCol = [datetime.datetime(2019, 10, 7, 0, 0),
datetime.datetime(2019, 10, 3, 0, 0),
datetime.datetime(2019, 10, 3, 0, 0),
datetime.datetime(2019, 10, 14, 0, 0),
datetime.datetime(2019, 10, 15, 0, 0)]
dateCol = [dt.strftime('%m/%d/%Y') for dt in dateCol]
print(dateCol)
您还可以更改 Excel 日期预览。
关于python - 如何更改日期列表中的日期格式(并在通过 openpyxl 保存到 excel 时保留该日期格式)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58450831/