最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

Python读取和保存json文件&在线从网络url上读取json文件

互联网 admin 4浏览 0评论

Python读取和保存json文件&在线从网络url上读取json文件

读取

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 = '.json'
classes = {int(key):value for (key, value) in requests.get(LABELS_URL).json().items()}

Python读取和保存json文件&在线从网络url上读取json文件

读取

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 = '.json'
classes = {int(key):value for (key, value) in requests.get(LABELS_URL).json().items()}

发布评论

评论列表 (0)

  1. 暂无评论