[英]Extracting coordinates values using dict python
我真的需要幫助
dict 值的 1 看起來像這樣。 (我突出顯示了 x 和 y 值) :
{
"objects": [
{
"featureId": "ckbrnqtf600jb0y6o1hsnanqj",
"schemaId": "ckbrnnh4b009w0y5h4nend1n3",
"title": "point",
"value": "point",
"color": "#FF0000",
"point": {"x": 1262, "y": 161},
"instanceURI": "https://api.labelbox.com/masks/feature/ckbrnqtf600jb0y6o1hsnanqj?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2JyaWh4b3JjYTBjMDc2Mml1OHR6Y3plIiwib3JnYW5pemF0aW9uSWQiOiJja2JyaWh4bnk0cnp1MDc3NjdmbmM1YXV4IiwiaWF0IjoxNTkyOTAzNDk0LCJleHAiOjE1OTU0OTU0OTR9.p1xWGMeXVJysOcYNjQbSHOlpnVdATkMwu-Ze-zm_wlg",
},
{
"featureId": "ckbrnqwdw0fbq0z808cxg5dbl",
"schemaId": "ckbrnnh4b009w0y5h4nend1n3",
"title": "point",
"value": "point",
"color": "#FF0000",
"point": {"x": 1320, "y": 162},
"instanceURI": "https://api.labelbox.com/masks/feature/ckbrnqwdw0fbq0z808cxg5dbl?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2JyaWh4b3JjYTBjMDc2Mml1OHR6Y3plIiwib3JnYW5pemF0aW9uSWQiOiJja2JyaWh4bnk0cnp1MDc3NjdmbmM1YXV4IiwiaWF0IjoxNTkyOTAzNDk0LCJleHAiOjE1OTU0OTU0OTR9.p1xWGMeXVJysOcYNjQbSHOlpnVdATkMwu-Ze-zm_wlg",
},
{
"featureId": "ckbrnqzfp00ji0y6ocz24ajah",
"schemaId": "ckbrnnh4b009w0y5h4nend1n3",
"title": "point",
"value": "point",
"color": "#FF0000",
"point": {"x": 1278, "y": 205},
"instanceURI": "https://api.labelbox.com/masks/feature/ckbrnqzfp00ji0y6ocz24ajah?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2JyaWh4b3JjYTBjMDc2Mml1OHR6Y3plIiwib3JnYW5pemF0aW9uSWQiOiJja2JyaWh4bnk0cnp1MDc3NjdmbmM1YXV4IiwiaWF0IjoxNTkyOTAzNDk0LCJleHAiOjE1OTU0OTU0OTR9.p1xWGMeXVJysOcYNjQbSHOlpnVdATkMwu-Ze-zm_wlg",
},
{
"featureId": "ckbrnr2vx0fos0y84dgi1ck5s",
"schemaId": "ckbrnnh4b009w0y5h4nend1n3",
"title": "point",
"value": "point",
"color": "#FF0000",
"point": {"x": 1328, "y": 210},
"instanceURI": "https://api.labelbox.com/masks/feature/ckbrnr2vx0fos0y84dgi1ck5s?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2JyaWh4b3JjYTBjMDc2Mml1OHR6Y3plIiwib3JnYW5pemF0aW9uSWQiOiJja2JyaWh4bnk0cnp1MDc3NjdmbmM1YXV4IiwiaWF0IjoxNTkyOTAzNDk0LCJleHAiOjE1OTU0OTU0OTR9.p1xWGMeXVJysOcYNjQbSHOlpnVdATkMwu-Ze-zm_wlg",
},
],
"classifications": [],
}
import json json1_file = open('/content/export-2020-06-30T12_57_16.744Z.json') json1_str = json1_file.read() json1_data = json.loads(json1_str) json1_data = json.loads(json1_str)[0] print("Type:", type(json1_data))
在代碼中,我將 JSON 轉換為 dict ,現在我必須提取值並將其保存在一行中。 請幫助代碼,我如何提取這些 x 和 y 值。
那不是那么困難。 我以縮短的形式添加了輸入數據:
dict= {"objects":[
{"featureId":"ckbrnqtf600jb0y6o1hsnanqj","schemaId":"ckbrnnh4b009w0y5h4nend1n3","title":"point","value":"point","color":"#FF0000",
"point":{"x":1262,"y":161},"instanceURI":"https:"},
{"featureId":"ckbrnqwdw0fbq0z808cxg5dbl","schemaId":"ckbrnnh4b009w0y5h4nend1n3","title":"point","value":"point","color":"#FF0000",
"point":{"x":1320,"y":162},"instanceURI":"https:"},
{"featureId":"ckbrnqzfp00ji0y6ocz24ajah","schemaId":"ckbrnnh4b009w0y5h4nend1n3","title":"point","value":"point","color":"#FF0000",
"point":{"x":1278,"y":205},"instanceURI":"https:"},
{"featureId":"ckbrnr2vx0fos0y84dgi1ck5s","schemaId":"ckbrnnh4b009w0y5h4nend1n3","title":"point","value":"point","color":"#FF0000",
"point":{"x":1328,"y":210},"instanceURI":"https:"}],"classifications":[]}
for d in dict["objects"]:
x = d["point"]["x"]
y = d["point"]["y"]
print(x,y)
輸出:
1262 161
1320 162
1278 205
1328 210
作為元組列表:
print([(d["point"]["x"], d["point"]["y"]) for d in dict["objects"]])
輸出:
[(1262, 161), (1320, 162), (1278, 205), (1328, 210)]
json
文件中的所有字典:
根據我可以從您發布的代碼中得出的信息,它應該是這樣的:
with open ('/content/export-2020-06-30T12_57_16.744Z.json', 'r') as jsonfile:
json1_str = jsonfile.read()
json1_data = json.loads(json1_str)
for dict in json1_data:
for d in dict["objects"]:
x = d["point"]["x"]
y = d["point"]["y"]
print(x,y) # demo, you may use it to create your own data strucure
# flat list of all (x,y) tuples of all dicts in the json file
print([(d["point"]["x"], d["point"]["y"]) for d in dict["objects"] for dict in json1_data])
你有一個對象數組,每個 object 都有點的鍵值對,同樣你有 x 和 y 的鍵值對,你可以像這樣訪問它:
for object in objects:
x = object["point"]["x"]
y = object["point"]["y"]
print("x = " + str(x) + " y = " + str(y))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.