2016-01-21 73 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"}]'