Python 在geoJSON上迭代
我的女儿看起来是这样的Python 在geoJSON上迭代,python,dictionary,geojson,Python,Dictionary,Geojson,我的女儿看起来是这样的 { "type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, "features"
{
"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]
}
}
]
}
我想访问属性
,并检查一些值
中的键
for features in geoJsonPoints["features"]:
for interesting in features["properties"]["value1"]:
print interesting
print "!"
我明白了
a
!
b
!
c
!
为什么?!好像我的循环没有给我一本字典
如果我这样做
for features in geoJsonPoints["features"]:
for interesting in features["properties"]:
print type(intereseting)
print interesting
我明白了
键入“unicode”
价值1
键入“unicode”
价值2
那为什么不是一本字典?而且,如果不是字典,为什么我可以访问“unicode”后面的值,就像我展示的第一个循环一样 features[“properties”][“value1”]
指向您逐个字符迭代的abc
字符串。相反,您可能打算迭代属性
字典:
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])
有关字典和循环技术的更多信息,请参见此处:
- 及
功能[“属性”][“值1”]
指向您逐个字符迭代的abc
字符串。相反,您可能打算迭代属性
字典:
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])
有关字典和循环技术的更多信息,请参见此处:
- 及
item()
呢?@Stophfaceitems()
(以及Python 2中的iteritems()
)允许您同时访问键和值。请按照发布的链接了解更多信息。谢谢。嗯,我想当我可以像在字典中的第一个循环那样进行迭代时,为什么我不能在嵌套循环中进行呢?@stopfacefeatures[“properties”]
在你的例子中是一个字典。如果您以的形式对其进行迭代,以获得特性[“属性”]
,那么您将只获得键(当然,您将能够以特性[“属性”][有趣的]
的形式访问值)。但是items()
允许您同时访问键和值。抱歉,我在解释和教学方面非常糟糕:)“如果你对它进行迭代以了解特性[“属性”],你将只获得键(当然,你将能够访问特性[“属性”][有趣]的值)。“这是有问题的,因为正如我的线程所示,它会逐字返回?!”?!为什么我需要item()
呢?@stopfaceitems()
(以及Python 2中的iteritems()
)允许您同时访问键和值。请按照发布的链接了解更多信息。谢谢。嗯,我想当我可以像在字典中的第一个循环那样进行迭代时,为什么我不能在嵌套循环中进行呢?@stopfacefeatures[“properties”]
在你的例子中是一个字典。如果您以的形式对其进行迭代,以获得特性[“属性”]
,那么您将只获得键(当然,您将能够以特性[“属性”][有趣的]
的形式访问值)。但是items()
允许您同时访问键和值。抱歉,我在解释和教学方面非常糟糕:)“如果你对它进行迭代以了解特性[“属性”],你将只获得键(当然,你将能够访问特性[“属性”][有趣]的值)。“这是有问题的,因为正如我的线程所示,它会逐字返回?!”?!