【发布时间】:2019-04-30 13:48:51
【问题描述】:
我想将我的数据帧转换为 json 并以漂亮的打印格式将其转储到文件中。 我试过了——
df.to_json(r'C:\users\madhur\Desktop\example.json,'orient = 'index')
上面的代码在 json 中转储,但在一行中。
我也试过了
import json
hello = df.to_json(orient = 'index')
with open(r'C:\users\madhur\Desktop\example.json','a') as f:
json.dump(hello,f,indent=4,sort_keys=True)
但上面的代码给出了单行输出,输出在双引号之前有一个'\'。
输出看起来像 -
"{\"17668620_INCOLL\":{\"DRWECNTRY\":\"新西兰........"
如果有人对此查询有任何建议、答案或需要更多信息,请发表评论/回答。
注意 - 我使用的是 Python 3.6
【问题讨论】:
-
你能发布一些你的数据框,你的一些数据,更多的 JSON 吗?
标签: python json pandas dataframe pretty-print