读取
import json a = json.load(open("instances_val2017.json", "r")) #此时a是一个字典对象
更快的方法
import mmcv a = mmcv.load('result.json')
保存
将字典保存成json
import json a = { "name": "dabao", "id":123, "hobby": { "sport": "basketball", "book": "python study" } } with open("new_json.json",'w',) as f: json.dump(a, f)
记住是dumps不是dump
如果有中文,要
with open("new_json.json",'w',) as f: json.dump(a, f, ensure_ascii=False)
不然会被编码成unicode格式
在线从网络url上读取json文件
LABELS_URL = 'https://s3.amazonaws.com/outcome-blog/imagenet/labels.json' classes = {int(key):value for (key, value) in requests.get(LABELS_URL).json().items()}
Python读取和保存json文件&在线从网络url上读取json文件
于 2021-06-24 09:34:06 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)