簡體   English   中英

如何刪除字典外大括號或將沒有大括號的它附加到列表中?

[英]How to remove dictionary outer curly bracket or append it without curly bracket into a list?

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 文件時是否有其他方法?

我嘗試對 .replace 或 remove 等 json 文件寫入提示進行功能操作,但這沒有用。

您顯示的所需輸出是一個字典,因此只需創建一個字典而不是列表。 這本字典的鍵是你的圖像名稱,例如"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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM