gpt4 book ai didi

python - Python 中的 JSON 转储在文件中写入换行符和回车符。

转载 作者:行者123 更新时间:2023-12-02 08:57:34 34 4
gpt4 key购买 nike

当我使用以下命令打开有效的 json 并将其写入文件时,它会将换行符和回车符写入数据中。

with open('logs/output.json', 'w') as outfile:
json.dump(json_data, outfile, indent=2, separators=(',', ': '))

output.json 看起来像这样:

{\r\n \"config\": {\r\n \"app\": {\r\n \"calendar\": {\r\n \"{tenant-key}calendar.reference }

如何防止这种情况发生?

最佳答案

json.dump(json_data, outfile, separators=(',', ':'))

仅当您希望在新行上缩进时才需要缩进关键字参数。通过这样做,您正在激活“ pretty-print ”。

关于python - Python 中的 JSON 转储在文件中写入换行符和回车符。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110610/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com