2 回答
TA贡献1804条经验 获得超8个赞
您需要在遍历时进行枚举data['features'],以便可以分配回正确的值
data['features'][0]仅分配给ph列表索引 0。
with open('filepath', 'r+') as f:
data = json.load(f)
for i, feature in enumerate(data['features']): # enumerate while iterating
print(feature['properties'])
if feature['properties']["ID"] == 2:
data['features'][i]['properties']["pH"]=10 # assign back to the correct index location
f.seek(0)
json.dump(data, f)
f.truncate()
TA贡献1887条经验 获得超5个赞
data['features'][0]
由 [0] 索引,因此修改data["features"]. 您希望它根据评估为True您的条件的索引进行修改feature['properties']["ID"] == 2。
尝试
for index, feature in enumerate(data['features']):
...
if feature['properties']["ID"] == 2:
data['features'][index]['properties']["pH"] = 10
...
添加回答
举报