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



    上一篇

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

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

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

    json.dumps(obj)

    1. import json
    2. person={
    3. "input_offset": "104.0, 117.0, 123.0",
    4. "input_offset_list": [104.0, 117.0, 123.0],
    5. "channels_order": "BGR",
    6. "pp_mode": "default",
    7. "in_graph": False,
    8. "dimensions": "1, 3, 224, 224",
    9. "_channels": 3,
    10. "_order": 1,
    11. "_height": 224,
    12. "_width": 224,
    13. "input_layout": "NCHW",
    14. "ipp": "PIL"
    15. }
    16. print(person)
    17. print(type(person))
    18. jsonstr=json.dumps(person)
    19. print(jsonstr)
    20. print(type(jsonstr))

    输出结果如下:

    1. {'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'}
    2. <class 'dict'>
    3. {"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"}
    4. <class 'str'>
    5. >>>

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

    2,解析json字符串

    1. import json
    2. person={
    3. "input_offset": "104.0, 117.0, 123.0",
    4. "input_offset_list": [105.0, 118.0, 124.0],
    5. "channels_order": "BGR",
    6. "pp_mode": "default",
    7. "in_graph": False,
    8. "dimensions": "1, 3, 224, 224",
    9. "_channels": 3,
    10. "_order": 1,
    11. "_height": 224,
    12. "_width": 224,
    13. "input_layout": "NCHW",
    14. "ipp": "PIL"
    15. }
    16. print(person)
    17. print(type(person))
    18. print('-'*20)
    19. #python to json
    20. jsonstr=json.dumps(person)
    21. print(jsonstr)
    22. print(type(jsonstr))
    23. print('-'*20)
    24. #json to python
    25. pythonobj=json.loads(jsonstr)
    26. print(pythonobj)
    27. print(type(pythonobj))
    28. print('-'*20)
    29. #find nodes
    30. print(pythonobj.keys())
    31. print(pythonobj.values())
    32. print(pythonobj["input_offset"])
    33. print(pythonobj["input_offset_list"][0])
    34. print(pythonobj["input_offset_list"][1])
    35. print(pythonobj["input_offset_list"][2])

    输出格式如下

    1. >>>
    2. ================== RESTART: C:/Users/T14/Desktop/test_case2.py =================
    3. {'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'}
    4. <class 'dict'>
    5. --------------------
    6. {"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"}
    7. <class 'str'>
    8. --------------------
    9. {'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'}
    10. <class 'dict'>
    11. --------------------
    12. dict_keys(['input_offset', 'input_offset_list', 'channels_order', 'pp_mode', 'in_graph', 'dimensions', '_channels', '_order', '_height', '_width', 'input_layout', 'ipp'])
    13. 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'])
    14. 104.0, 117.0, 123.0
    15. 105.0
    16. 118.0
    17. 124.0

  • 相关阅读:
    【Leetcode】478. Generate Random Point in a Circle(配数学证明)
    Java知识体系索引
    变电站远程监控维护,工程师不用出差跑断腿
    Linux_Shell运行原理(命令行解释器)
    UE4 Unlua源码解析10 - Lua怎么替换BlueprintImplementableEvent或BlueprintNativeEvent的方法实现的
    NIO总结文——一篇读懂NIO整个流程
    Markdown和PlantUML的基本使用
    学习JS闭包
    Java语言知识大盘点(期末总复习)三
    嵌入式UBoot如何跳转Kernel—uboot与linux交界分析
  • 原文地址:https://blog.csdn.net/Cupid99/article/details/126167184