栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python学习之CSDN21天学习挑战赛计划之4

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python学习之CSDN21天学习挑战赛计划之4

​
​上一篇

活动地址:CSDN21天学习挑战赛

学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。

1,继续学习json模块的用法,举个例子:

json.dumps(obj)

import json
person={
    "input_offset": "104.0, 117.0, 123.0",
    "input_offset_list": [104.0, 117.0, 123.0],
    "channels_order": "BGR",
    "pp_mode": "default",
    "in_graph": False,

    "dimensions": "1, 3, 224, 224",
    "_channels": 3,
    "_order": 1,
    "_height": 224,
    "_width": 224,

    "input_layout": "NCHW",
    "ipp": "PIL"
    }
print(person)
print(type(person))
jsonstr=json.dumps(person)
print(jsonstr)
print(type(jsonstr))

输出结果如下:

{'input_offset': '104.0, 117.0, 123.0', 'input_offset_list': [104.0, 117.0, 123.0], 'channels_order': 'BGR', 'pp_mode': 'default', 'in_graph': False, 'dimensions': '1, 3, 224, 224', '_channels': 3, '_order': 1, '_height': 224, '_width': 224, 'input_layout': 'NCHW', 'ipp': 'PIL'}

{"input_offset": "104.0, 117.0, 123.0", "input_offset_list": [104.0, 117.0, 123.0], "channels_order": "BGR", "pp_mode": "default", "in_graph": false, "dimensions": "1, 3, 224, 224", "_channels": 3, "_order": 1, "_height": 224, "_width": 224, "input_layout": "NCHW", "ipp": "PIL"}

>>> 

也就是说带s的都是和字符串相关的,不带s的都是和文件相关的。

2,解析json字符串

import json
person={
    "input_offset": "104.0, 117.0, 123.0",
    "input_offset_list": [105.0, 118.0, 124.0],
    "channels_order": "BGR",
    "pp_mode": "default",
    "in_graph": False,

    "dimensions": "1, 3, 224, 224",
    "_channels": 3,
    "_order": 1,
    "_height": 224,
    "_width": 224,

    "input_layout": "NCHW",
    "ipp": "PIL"
    }
print(person)
print(type(person))
print('-'*20)
#python to json
jsonstr=json.dumps(person)
print(jsonstr)
print(type(jsonstr))
print('-'*20)
#json to python
pythonobj=json.loads(jsonstr)
print(pythonobj)
print(type(pythonobj))
print('-'*20)
#find nodes
print(pythonobj.keys())
print(pythonobj.values())
print(pythonobj["input_offset"])
print(pythonobj["input_offset_list"][0])
print(pythonobj["input_offset_list"][1])
print(pythonobj["input_offset_list"][2])
输出格式如下
>>> 
================== RESTART: C:/Users/T14/Desktop/test_case2.py =================
{'input_offset': '104.0, 117.0, 123.0', 'input_offset_list': [105.0, 118.0, 124.0], 'channels_order': 'BGR', 'pp_mode': 'default', 'in_graph': False, 'dimensions': '1, 3, 224, 224', '_channels': 3, '_order': 1, '_height': 224, '_width': 224, 'input_layout': 'NCHW', 'ipp': 'PIL'}

--------------------
{"input_offset": "104.0, 117.0, 123.0", "input_offset_list": [105.0, 118.0, 124.0], "channels_order": "BGR", "pp_mode": "default", "in_graph": false, "dimensions": "1, 3, 224, 224", "_channels": 3, "_order": 1, "_height": 224, "_width": 224, "input_layout": "NCHW", "ipp": "PIL"}

--------------------
{'input_offset': '104.0, 117.0, 123.0', 'input_offset_list': [105.0, 118.0, 124.0], 'channels_order': 'BGR', 'pp_mode': 'default', 'in_graph': False, 'dimensions': '1, 3, 224, 224', '_channels': 3, '_order': 1, '_height': 224, '_width': 224, 'input_layout': 'NCHW', 'ipp': 'PIL'}

--------------------
dict_keys(['input_offset', 'input_offset_list', 'channels_order', 'pp_mode', 'in_graph', 'dimensions', '_channels', '_order', '_height', '_width', 'input_layout', 'ipp'])
dict_values(['104.0, 117.0, 123.0', [105.0, 118.0, 124.0], 'BGR', 'default', False, '1, 3, 224, 224', 3, 1, 224, 224, 'NCHW', 'PIL'])
104.0, 117.0, 123.0
105.0
118.0
124.0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033406.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号