如何删除字典外大括号或将其不带大括号附加到列表中?
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 文件时是否有其他方法?
我尝试对 json 文件写入提示进行功能操作,例如 .replace 或 remove,但这没有用。
您显示的所需输出是字典,因此只需创建字典而不是列表。这个字典的键是你的图像名称,例如 "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]
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,但这没有用。
您显示的所需输出是字典,因此只需创建字典而不是列表。这个字典的键是你的图像名称,例如 "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]