我有一個 .json 檔案,前幾行是:
{
"global_id": "HICO_train2015_00000001",
"hois": [
{
"connections": [
[
0,
0
]
],
"human_bboxes": [
[
207,
32,
426,
299
]
],
"id": "153",
"invis": 0,
"object_bboxes": [
[
58,
97,
571,
404
]
]
},
我想列印human_bboxes。id 和 object_bboxes。
我試過這個代碼:
import json
# Opening JSON file
f = open('anno_list.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
s=data[0]
for i in s:
print(i[1])
# Closing file
f.close()
但是,它給了我這個輸出:
l
o
m
m
uj5u.com熱心網友回復:
做這個:
import json
# Opening JSON file
f = open('anno_list.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
s=data[0]
# Do This:
hois_data = s["hois"][0]
print("human_bboxes",hois_data["human_bboxes"])
print("id",hois_data["id"])
print("object_bboxes",hois_data["object_bboxes"])
# Closing file
f.close()
uj5u.com熱心網友回復:
Behdad Abdollahi Moghadam 的答案會正確列印答案,但僅適用于一組 bboxes 和 id。下面的答案還有一個 for 回圈,它決議整個檔案并將所有人類和物件 bboxes 和 id 列印到一個檔案中。
import json
# Opening JSON file
f = open('anno_list.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
f1 = open("file1.txt", "w")
for annotation in data:
f1.write("==============\n")
f1.write(annotation["global_id"])
for hois in annotation["hois"]:
f1.write("\n")
f1.write("---")
f1.write("\n")
f1.write(hois["id"])
f1.write("\n")
f1.write(str(hois["human_bboxes"]))
f1.write("\n")
f1.write(str(hois["object_bboxes"]))
f1.write("\n")
# Closing file
f.close()
f1.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/369602.html
上一篇:即回傳')'預期時
