上一篇
活动地址: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



