如果“ID”匹配,我试图更改特定的嵌套值(“pH”),但它只更改第一个而不是我想要的那个。
我想做什么:
{
"type": "FeatureCollection",
"name": "test",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::3059"
}
},
"features": [{
"type": "Feature",
"properties": {
"ID": 1,
"pH": 3.5,
"P": 2.8,
"K": 11.0,
"Mg": 15.8
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
]
]
}
}, {
"type": "Feature",
"properties": {
"ID": 2,
"pH": 3,
"P": 2.5,
"K": 11.1,
"Mg": 15.8
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
]
]
}
}
]
}
但它改变了“ID”为 1 的“pH 值”和“ID”:2 的“pH”值保持不变。
这是我的代码:
import json
with open('filepath', 'r+') as f:
data = json.load(f)
for feature in data['features']:
print(feature['properties'])
if feature['properties']["ID"] == 2:
data['features'][0]['properties']["pH"]=10
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()
胡说叔叔
慕工程0101907
相关分类