保存中文格式json
js = ['测试']
with open('./test','w',encoding='utf-8') as f:
f.write(json.dumps(js, ensure_ascii=False))
json文件追加案例
json 文件需要,读取全部内容,变成列表,清空原来文件,重新写入文件
import os
filename = r'./test.json'
if os.path.exists(filename):
print('已存在json文件,追加')
with open(filename, 'r', encoding='utf8') as history_file:
history_file_list = json.load(history_file)
with open(filename, 'w+', encoding='utf8') as history_file_write:
new_order = {}
history_file_list.append(new_order)
history_file_write.write(json.dumps(history_file_list, ensure_ascii=False))
else:
print('无json文件创建。。。')
new_order_list = []
new_order = {}
new_order_list.append(new_order)
with open(filename, 'w+', encoding='utf8') as f:
f.write(json.dumps(new_order_list, ensure_ascii=False))
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did169099