Mad*_*dav 7 python json pretty-print dataframe pandas
我想将我的数据帧转换为 json 并将其转储到一个漂亮的打印格式的文件中。我试过 -
df.to_json(r'C:\users\madhur\Desktop\example.json,'orient = 'index')
Run Code Online (Sandbox Code Playgroud)
上面的代码在 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)
Run Code Online (Sandbox Code Playgroud)
但上面的代码给出了单行输出,输出'\'在双引号前有一个。
输出看起来像 -
"{\"17668620_INCOLL\":{\"DRWECNTRY\":\"新西兰......"
如果有人有任何建议、答案或需要有关此查询的更多信息,请发表评论/回答。
注意 - 我使用的是 Python 3.6
小智 6
你可以直接通过indent = int:
df.to_json(r'example.json', orient='index', indent=2 )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4120 次 |
| 最近记录: |