我有一個包含 20 個 .png 檔案和一個 .json 檔案的檔案夾。.json 檔案如下所示
{
"ID":12,
"flags"={},
"shapes":[
{"label":"text",
"points":[[65, 14],[27, 40]],
}
],
"flags"={},
"shapes":[
{"label":"logo",
"points":[[165, 124],[207, 43]],
}
],
"flags"={},
"shapes":[
{"label":"text",
"points":[[54, 24],[17, 53]],
}
]
}
我想列出每個檔案中的所有“標簽”。我怎樣才能做到這一點?
我試過
import os
import json
path_to_json = './'
contents=[]
for file_name in [file for file in os.listdir(path_to_json) if file.endswith('.json')]:
with open(path_to_json file_name) as json_file:
data=json.load(json_file)
contents.append(data)
到這里為止看起來還不錯,現在我需要獲取“標簽”的值,以下部分不起作用
l=[]
for i in range(len(contents)):
label= contents[i]['shapes']['label']
l.append(label)
print(l)
uj5u.com熱心網友回復:
shapes是一個串列,因此您必須對其進行迭代:
l = []
for content in contents:
for shape in content['shapes']:
l.append(shape['label'])
print(l)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477518.html
上一篇:你能幫我理解這段Java代碼嗎?
下一篇:用鍵分隔行并存盤在不同的檔案中
