美文网首页
python解析json文件

python解析json文件

作者: 有事没事扯扯淡 | 来源:发表于2019-12-11 09:53 被阅读0次

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。在机器学习和深度学习中,解析标签文件时常常用到json文件。利用python进行标签解析是非常常见的,此处进行简单记录。json文件如下所示:

    {
      "version": "3.16.7",
      "flags": {},
      "shapes": [
        {
          "label": "scab",
          "line_color": null,
          "fill_color": null,
          "points": [
            [
              24.307692307692307,
              282.8131868131868
            ],
            [
              515.5164835164835,
              787.2087912087911
            ]
          ],
          "shape_type": "rectangle",
          "flags": {}
        },
        {
          "label": "scab",
          "line_color": null,
          "fill_color": null,
          "points": [
            [
              529.8021978021978,
              134.46153846153845
            ],
            [
              960.5714285714284,
              858.6373626373626
            ]
          ],
          "shape_type": "rectangle",
          "flags": {}
        },
        {
          "label": "scab",
          "line_color": null,
          "fill_color": null,
          "points": [
            [
              964.967032967033,
              304.79120879120876
            ],
            [
              1367.164835164835,
              704.7912087912088
            ]
          ],
          "shape_type": "rectangle",
          "flags": {}
        }
      ],
      "lineColor": [
        0,
        255,
        0,
        128
      ],
      "fillColor": [
        255,
        0,
        0,
        128
      ],
      "imagePath": "TriggerQS-15208921-152.jpg",
      "imageData": XXXXXXX(数据)
    

    利用python对其进行解析时可使用json依赖包:

    import json
    
    with open(file_json, 'r', encoding='utf-8') as f:
        ret_dic = json.load(f)
        print(ret_dic['shapes'][0]['label'])
        print(ret_dic['shapes'][0]['points'])
    

    scab
    [[159.47252747252747, 327.86813186813185], [459.4725274725274, 702.5934065934066]]

    获取数据只需要关键字就可以了,结果返回的式list或者dict。

    相关文章

      网友评论

          本文标题:python解析json文件

          本文链接:https://www.haomeiwen.com/subject/sfzngctx.html