我有以下代碼,我用它來打開上述json檔案并提取資料。然而,我只想要坐標資料,但代碼給了我以下錯誤。
TypeError: string indices must be integers.
我怎樣才能只列印出坐標資料呢?
{
"type": "多邊形"。
"坐標": [
[
[
-5.84731,
60.5832.
],
[
-5.93843,
60.58322.39097,
60.58322.39097,
60.58432.75097,
60.5823import json
f = open('allData.json'/span>)
data = json.load(f)
print(data['type']['coordinate'] )
f.close()
uj5u.com熱心網友回復:
print(data['type']['coordinate'] )
這是先獲取data['type'],="Polygon",然后對其應用索引['coordinate'],因此你得到的關于索引字串的錯誤。
coordinates(復數)在你的結構中不是type的一個子項。你需要data['coordinates']。
uj5u.com熱心網友回復:
你只需要呼叫coordinates
import json
f = open('allData.json'/span>)
data = json.load(f)
print(data['coordinates'] )
f.close()
輸出
[[-5.84731, 60.5832], [-5.93843, 60. 5832], [-2.39097, 60.5832], [-2. 39097, 60.5843], [-2.75097, 60.5823] ] ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/306907.html
標籤:
下一篇:過濾物件內的陣列值
