我只是想知道,我如何强制python停止将我的.json
文件写入一行?我不知道该怎么办,所以我在这里问。我已经尝试将写入命令with open('manifest.json', 'wb') as f:
更改为with open('manifest.json', 'w') as f:
,但它仍在写入一行?
import json
with open('manifest.json', 'r') as f:
json_data = json.load(f)
json_data['server-version'] = "xxxx-x.x.x"
with open('manifest.json', 'w') as f:
f.write(json.dumps(json_data))
print('Successful.')
如果我在询问时出错,请更正。我是stackoverflow的新手:)
答案 0 :(得分:12)
对于漂亮的打印使用缩进参数
print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4, separators=(',', ': ')))