Python-如何将JSON数据写入文件?

Python-如何将JSON数据写入文件?,第1张

Python-如何将JSON数据写入文件?

你忘记了实际的JSON部分- data是字典,尚未进行JSON编码。写这样的最大兼容性(Python 2和3):

import jsonwith open('data.json', 'w') as f:    json.dump(data, f)

在现代系统(即Python 3和UTF-8支持)上,你可以使用

import jsonwith open('data.json', 'w', encoding='utf-8') as f:    json.dump(data, f, ensure_ascii=False, indent=4)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4985342.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存