2016-01-21 70 views
2

以下是我尝试使用pandas read_excelto_json函数将其转换为JSON的excel中的数据。 JSON日期的字段"Date"1449446400000(不含引号)。我想知道为什么日期显示为一个大数字而不是12/7/2015Python Pandas read_excel和to_json日期格式错误

ID Date  Name Lat  Long  Pick Success Failure Drop Amount 
=========================================================================== 
5 12/7/2015 PSG 11.0231335 77.0016396 31 21  10 44 5192       

请让我知道如何将它转换成适当的JSON格式的日期格式,以便我可以用它来生成一些JavaScript图表。

下面是代码片段;

def home(request): 
    df = pandas.read_excel('<here goes the excel path>') 
    json = df.to_json(orient="records") 
    return render(request, 'home.html', {'data':json}) 

感谢

回答

2

你必须书写时与以JSON设置date_format

json = df.to_json(orient="records", date_format='iso') 

由于默认的是 '时代',没有它设置明确地以 'ISO',你”重新获得你的结果在纪元毫秒。这返回样本输出:

'[{"id":5,"date":"2015-07-12T00:00:00.000Z"}]'