Python处理JSON文件
发布时间
阅读量:
阅读量
读json
import json
json_file = ''
with open(json_file, 'r', encoding='utf8') as fp:
json_data = json.load(fp)
写json
import json
json_file = '1.json'
dict1 = [{'name': 'Tom', 'age': 10}, {'name': 'Marry', 'age': 18}]
with open(json_file,'w',encoding='utf8')as fp:
json.dump(dict1,fp,ensure_ascii=False)
r:仅限于只读模式(若要使用带有r权限的文件,则该文件必须事先存在)。
r+:可读可写,并不会因尝试创建不存在而引发错误(若要使用带有r+权限的文件,则该文件必须事先存在)。当直接向其写入数据时,默认从当前位置开始覆盖;若在已存在的该文件末尾进行追加,则需先对其读取以获取当前位置。
w:仅允许追加(append)操作。若无此文件,则初始化为空。
w+:支持读取与追加(append)操作。若该文件存在,则将其内容全部替换;若无此文件,则初始化
全部评论 (0)
还没有任何评论哟~
