【发布时间】: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