问题
Below is the data from an excel which I am trying to convert to JSON using pandas read_excel
and to_json
functions. The JSON date has the field "Date"
as 1449446400000
(without quotes). I am wondering why the date is displayed as a big number instead of 12/7/2015
.
ID Date Name Lat Long Pick Success Failure Drop Amount
===========================================================================
5 12/7/2015 PSG 11.0231335 77.0016396 31 21 10 44 5192
Please let me know how to convert it into a proper date format in JSON so that I can use to to generate some JavaScript charts.
Below is the code snippet;
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})
Thanks
回答1:
You have to set the date_format
when writing to json with:
json = df.to_json(orient="records", date_format='iso')
Since the default is 'epoch', without setting it explicity to 'iso', you're getting your results in epoch milliseconds. This returns for a sample output:
'[{"id":5,"date":"2015-07-12T00:00:00.000Z"}]'
来源:https://stackoverflow.com/questions/34920285/python-pandas-read-excel-and-to-json-date-format-error