我得到了数据库搜索的结果,其中某些列有日期,但其他列没有。我需要帮助来写作
data = [['556644', 'Mr', 'So', 'And', 'So', Decimal('0.0000'), datetime.datetime(2012, 2, 25, 0, 0), '', False, datetime.datetime(2013, 6, 30, 0, 0)],...]
导入 Excel 电子表格,以便
easyxf(num_format_str='DD/MM/YYYY')
仅适用于日期时间列。我对 Python 还很陌生,这几天我一直在思考这个问题。谢谢!
最佳答案
简化你的 self 回答后:
date_xf = easyxf(num_format_str='DD/MM/YYYY') # sets date format in Excel
data = [list(n) for n in cursor.fetchall()]
for row_index, row_contents in enumerate(data):
for column_index, cell_value in enumerate(row_contents):
if isinstance(cell_value, datetime.date):
sheet1.write(row_index+1, column_index, cell_value, date_xf)
else:
sheet1.write(row_index+1, column_index, cell_value)
关于python-2.7 - 仅使用 xlwt 将日期格式应用于日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11185104/