- json 中文编码出错,比如{"sc": "\u9644\u67d1\u4e52"}
ensure_ascii = False 加上这个就好了,这个是看的别人的博客上的,感谢
with open('demo.json', 'a', encoding = 'utf-8') as f:
json.dump(data, f, ensure_ascii = False)
- 以一种易读的格式写入json文件
data = {'name': '小敏', 'age': 20}
data_neat = json.dumps(data, indent = 4, ensure_ascii = False)
# {
# "name": "小敏",
# "age": 20
# }
with open('demo.json', 'a', encoding = 'utf-8') as f:
print(data_neat, file = f)
- json文件或txt文件里的每一行是一个json格式如何读取: