如何克服“TypeError:datetime类型的对象不可JSON序列化”错误?

2024-06-07 14:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

当我将数据从excel导出到Google电子表格时,我得到了TypeError: Object of type 'datetime' is not JSON serializable错误

我怎样才能解决这个问题?你能帮帮我吗

代码:

xlApp = win32.Dispatch('Excel.Application')
wb = xlApp.Workbooks.Open("C:/Users/User/Desktop/ARCalc_05.2020 -.xlsx")
ar_excel = wb.Worksheets('AR').Range('A1').CurrentRegion()
data_excel = wb.Worksheets('Data').Range('A1').CurrentRegion()

aged_rec_gsheet = gc.open_by_url("https://docs.google.com/spreadsheets/d/1DIdwwdZ_Vk0OJQPohGBJvdVDBVlRMO9I-Sb6foNLmD8/edit#gid=1908610852")

aged_rec_gsheet.values_clear('Data!A:G')
aged_rec_gsheet.values_update(
'Data!A2', 
params={'valueInputOption': 'USER_ENTERED', 
        'DateTimeRenderOption':'FORMATTED_STRING'}, 
body={'values': data_excel})

我得到的错误是:

body={'values': data_excel}
TypeError: Object of type datetime is not JSON serializable

我尝试了一些方法来解决这个问题(比如创建一个JSONECODER类和函数),但没有成功

谢谢


Tags: ofdatadatetimeobjectistypenotexcel

热门问题