我的geoJSON看起来像这样
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [{
"type": "Feature",
"properties": {
"value1": "abc",
"value2": 0,
"value3": 0.99,
"value4": "def",
"value5": "882.3",
"value6": 12,
},
"geometry": {
"type": "Point",
"coordinates": [1, 1]
}
}
]
}
我想访问properties
并检查一些values
以查找key
for features in geoJsonPoints["features"]:
for interesting in features["properties"]["value1"]:
print interesting
print "!"
我得到了
一
<!/ P>
B'/ P>
<!/ P>
C
<!/ P>
为什么?!好像我的循环没有给我一个字典?!
如果我这样做
for features in geoJsonPoints["features"]:
for interesting in features["properties"]:
print type(intereseting)
print interesting
我得到了
输入'unicode'
值1
输入'unicode'
value2
...
为什么不是字典?而且,如果它不是字典,为什么我可以访问“unicode”背后的值,就像我展示的第一个循环一样?!
答案 0 :(得分:2)
features["properties"]["value1"]
指向abc
字符串,您逐个字符地迭代。相反,您可能想要遍历properties
字典:
for property_name, property_value in features["properties"].items():
print(property_name, property_value)
或者,您可以遍历字典键:
for property_name in features["properties"]:
print(property_name, features["properties"][property_name])
在此处查看有关字典和循环技术的更多信息: