json 파일에서의 특정 값만 추출
조회수 1371회
{
"1.png896616":{
"filename":"1.png",
"size":896616,
"regions":[
{
"shape_attributes":{
"name":"polygon",
"all_points_x":[
286,284,......
],
"all_points_y":[
94,93,....
]
},
"region_attributes":{
"sweet_pepper":"YELLOW"
}
},
{
"shape_attributes":{
"name":"polygon",
"all_points_x":[
455,457,...
],
"all_points_y":[
194,202,...
]
},
"region_attributes":{
"sweet_pepper":"YELLOW"
}
},
{
"shape_attributes":{
"name":"polygon",
"all_points_x":[
295,294,...
],
"all_points_y":[
426,437,...
]
},
"region_attributes":{
"sweet_pepper":"RED"
}
}
],
"file_attributes":{}
},
"2.png810690":{
"filename":"2.png",
"size":810690,
"regions":[
{
"shape_attributes":{
"name":"polygon",
"all_points_x":[
257,246,...
],
"all_points_y":[
18,16,...
]
},
"region_attributes":{
"sweet_pepper":"YELLOW"
}
},
{
"shape_attributes":{
"name":"polygon",
"all_points_x":[
439,466,...
],
"all_points_y":[
93,115,...
]
},
"region_attributes":{
"sweet_pepper":"YELLOW"
}
},
{
"shape_attributes":{
"name":"polygon",
"all_points_x":[
283,284...
],
"all_points_y":[
355,381,...
]
},
"region_attributes":{
"sweet_pepper":"RED"
}
},
{
"shape_attributes":{
"name":"polygon",
"all_points_x":[
193,176,...
],
"all_points_y":[
169,174,...
]
},
"region_attributes":{
"sweet_pepper":"GREEN"
}
}
],
"file_attributes":{
}
}
}
json 파일 형식은 이렇습니다. 여기서는 2개의 이미지에 대한 json파일만 넣어놨지만, 실제로는 40개가 넘습니다.
저는 all_points_x, all_points_y에 대한 값들만 뽑아내고 싶은데 방법을 어떻게 해야할 지 모르겠어요. all_points_x, all_points_y의 값은 1.png896616(혹은 2.png810690) -> regions -> shape_attributes 안에 포함되어있습니다.
이미지가 1개일 경우에 all_points_x와 all_points_y는 추출했는데 이게 2개 이상 되어버리니까 어떻게 해야할 지 모르겠어요.
이미지 1개마다 따로따로 추출해야하나요?
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
import json from os import readlink with open('json 경로', 'r') as f: json_data = json.load(f) keyList = json_data.keys() key_list = list(keyList) #print(key_list) for e in range(2): key = key_list[e] print(key) for i, j in enumerate(json_data[key]['regions']): print("all_poins_x : ", i,j['shape_attributes']['all_points_x']) print("all_poins_y : ", i,j['shape_attributes']['all_points_y'])
로 했습니다. 혹시 참고하실 분 있으면 보세용!
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
- 딕셔너리는 items() 로 이터레이트 하는 게 더 깔끔해요. for k, v in json_data.items(): 이렇게요. nowp 2021.5.25 09:03
- 오 items가 있었네요 참고할게요! 알 수 없는 사용자 2021.5.25 10:40
댓글 입력