【问题标题】:How to remove dictionary outer curly bracket or append it without curly bracket into a list?如何删除字典外部大括号或将其不带大括号附加到列表中?
【发布时间】:2021-12-06 10:34:54
【问题描述】:
data = [

]
for x in range(0, len(directory_files)):
    # print(directory_files[x])
    # print(os.stat('Data\\' + directory_files[x]).st_size)
    img = ("Data\\" + directory_files[x])
    img_with_no_tag = directory_files[x]
    size_of_img = str(os.stat('Data\\' + directory_files[x]).st_size)
    img_capsulated = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
    img_capsulated_rgb = cv2.imread(img, cv2.IMREAD_COLOR)
    parent_tag = img_with_no_tag + size_of_img
    contourIndexNo = 0
    listTest = {
        img_with_no_tag + size_of_img: {
            'filename': img_with_no_tag,
            'size': size_of_img,
            'regions': [
            ],
            'file_attributes': {
                'caption': '',
                'public_domain': 'no',
                'image_url': ''
            }
        }
    }
    # print(*listTest, sep="{}")
    data.append(listTest)

这就是我执行代码的方式。输出如下所示:

[
{
    "138_mask.png4776": {
        "filename": "138_mask.png",
        "size": "4776",
        "regions": [
            {
                "shape_attributes": {
                    "name": "polygon",
                    "all_points_x": [
                        299,
                        294,
                        255,
                        245,
                        227,
                        229,
                        207,
                        200,
                        219,
                        209,
                        248,
                        251,
                        330,
                        341,
                        394,
                        407,
                        441,
                        447,
                        475,
                        485,
                        424,
                        438
                    ],
                    "all_points_y": [
                        517,
                        540,
                        534,
                        610,
                        610,
                        772,
                        778,
                        838,
                        843,
                        922,
                        928,
                        949,
                        955,
                        937,
                        943,
                        825,
                        827,
                        795,
                        799,
                        714,
                        673,
                        556
                    ]
                },
                "region_attributes": {
                    "name": "rooftop",
                    "type": "rooftop",
                    "image_quality": {
                        "good": true
                    }
                }
            }
        ],
        "file_attributes": {
            "caption": "",
            "public_domain": "no",
            "image_url": ""
        }
    }
},
{----------------------------------------> cannot remove it
    "332_mask.png8391": {
        "filename": "332_mask.png",
        "size": "8391",
        "regions": [
            {

....

但是,我想得到这样的期望输出:

"138_mask.png4776": {
    "filename": "138_mask.png",
    "size": 4776,
    "regions": [
        {
            "shape_attributes": {
                "name": "polygon",
                "all_points_x": [
                    214,
                    209,
                    218,
                    200,
                    209,
                    223,
                    252,
                    300,
                    433,
                    455,
                    476,
                    407,
                    398,
                    328,
                    250
                ],
                "all_points_y": [
                    921,
                    921,
                    843,
                    835,
                    782,
                    613,
                    535,
                    518,
                    552,
                    680,
                    794,
                    861,
                    948,
                    962,
                    953
                ]
            },
            "region_attributes": {
                "name": "not_defined",
                "type": "unknown",
                "image_quality": {
                    "good": true,
                    "frontal": true,
                    "good_illumination": true
                }
            }
        }
    ],
    "file_attributes": {
        "caption": "",
        "public_domain": "no",
        "image_url": ""
    }
},
"332_mask.png8391": {  ------------------------------> here what I want
    "filename": "332_mask.png",
    "size": 8391,
    "regions": [
        {
            "shape_attributes": {
                "name": "polygon",
                "all_points_x": [
                    137,
                    171,
                    395,
                    361
                ],
                "all_points_y": [
                    515,
                    389,
                    437,
                    567
                ]
            },
            "region_attributes": {
                "name": "not_defined",
                "type": "unknown",
                "image_quality": {
                    "good": true,
                    "frontal": true,
                    "good_illumination": true
                }
            }
        },
        {
            "shape_attributes": {
                "name": "polygon",
                "all_points_x": [
                    567,
                    586,
                    814,
                    794
                ],
                "all_points_y": [
                    605,
                    464,
                    492,
                    636
                ]
            },
            "region_attributes": {
                "name": "not_defined",
                "type": "unknown",
                "image_quality": {
                    "good": true,
                    "frontal": true,
                    "good_illumination": true
                }
            }
        },
        {
            "shape_attributes": {
                "name": "polygon",
                "all_points_x": [
                    451,
                    464,
                    739,
                    729
                ],
                "all_points_y": [
                    833,
                    677,
                    694,
                    845
                ]
            },
            "region_attributes": {
                "name": "not_defined",
                "type": "unknown",
                "image_quality": {
                    "good": true,
                    "frontal": true,
                    "good_illumination": true
                }
            }
        }
    ],
    "file_attributes": {
        "caption": "",
        "public_domain": "no",
        "image_url": ""
    }
}

}

这意味着,在将字典值附加到列表中时,我想将它们放入,因为它们不是单独的对象,或者在将它们写入 json 文件时是否有替代方法?

我尝试对 json 文件写入提示进行功能操作,例如 .replace 或 remove,但没有奏效。

【问题讨论】:

  • 你是如何生成输出的?如果使用看起来像你的json 模块,你应该明确说明是这种情况。
  • 以下评论解决了问题,但是,我想就您的评论提供反馈。我使结构组织得不好。正如@Rabinzel 的澄清,我改变了骨架,现在它运行良好。感谢您的贡献。

标签: python json list dictionary append


【解决方案1】:

您显示为所需输出的是字典,因此只需创建字典而不是列表。那么这个字典的键就是你的图片名称,比如"138_mask.png4776"

data = {}

然后稍后在您的代码中将条目添加到字典中:

data[img_with_no_tag + size_of_img] = {
    'filename': img_with_no_tag,
    'size': size_of_img,
    'regions': [],
    'file_attributes': {
        'caption': '',
        'public_domain': 'no',
        'image_url': ''
    }
}

最后,如果你想把这本字典放在一个列表中,你总是可以这样做:

data = [data]

【讨论】:

  • 如果我理解你的目标是正确的,@Jan Willem 做了正确的事。目前你有一个列表,该列表中的每一项都是一个字典。您的列表长度为 2(138_mask.png4776 和 332_mask.png8391),这就是为什么不能像在输出错误的代码中那样删除 ',' 和 '{' 的原因。您想要一个包含一个项目的列表,其中包含一个带有 2 个键(138.png 和 332.png)的字典
  • 非常感谢@Jan Willem 和 Rabinzel(抱歉没有注释,系统限制了我),现在它可以完美运行了。我试图执行一个 sn-p 将二进制掩码的对象坐标转换为 JSON 文件,以便以相反的顺序为将来的注入(多类和单类)提供 MaskRCNN 模型。我试图找到预制代码,但我找不到,我决定自己做。谢谢大家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2019-06-11
  • 2017-04-20
  • 2010-12-26
  • 1970-01-01
  • 2021-02-26
相关资源
最近更新 更多