每当我将dataFrame转换为_json时,任何日期格式都会更改为“ 纪元时间”
[{"idNo":1234567891012,"Name":"Jack","genderType":"male","Date":1544572800000,"branchName":"NY","location":"loc1","pCode":123}]
原日期为
Date:2018-12-12
我的python代码
@app.route("/fileviewer/" , methods=["GET" , "POST"])
def fileviewer(name):
dest = (file_destination)
df = pd.read_excel(dest)
print(df)
x1=df.to_json(orient ='records')
print(x1)
render_template('fileviewer.html',x=df.to_html())
return render_template('fileviewer.html',x=x1)
df
打印良好
x1
带有“纪元时间”打印
答案 0 :(得分:0)
您可以使用选项date_format
更改格式:
>>> df = pandas.DataFrame([
{'A' : 0, 't' : datetime.datetime(2018, 1, 2)},
{'A' : 1, 't' : datetime.datetime(2018, 1, 5)},
{'A' : 2, 't' : datetime.datetime(2018, 1, 7)}
])
>>> df.to_json(date_format = 'iso')
'{"A":{"0":0,"1":1,"2":2},"t":{"0":"2018-01-02T00:00:00.000Z","1":"2018-01-05T00:00:00.000Z","2":"2018-01-07T00:00:00.000Z"}}'
答案 1 :(得分:0)
我找到了一个很好的解决方案here
df.assign(
**df.select_dtypes(['datetime']).astype(str).to_dict('list')
).to_json(orient="records")