简体   繁体   English

遍历包含嵌套元素的dict对象

[英]Iterating over a dict object that contains nested elements

Python 5.6 Python 5.6

Here is the result from a call using the geocoder module 这是使用地址解析器模块进行调用的结果

import geocoder
anaddress = 'State Street, Hood River, OR'
g = geocoder.arcgis(anaddress)
d = g.geojson
print(d)
{'geometry': {'type': 'Point', 'coordinates': [-121.52181774656506, 45.707876183969184]}, 'type': 'Feature', 'properties':
 {'provider': 'arcgis', 'ok': True, 'location': '1037 State St, Hood River, OR', 'lat': 45.707876183969184, 'lng': -121.52
181774656506, 'bbox': [-121.52281774656507, 45.706876183969186, -121.52081774656506, 45.70887618396918], 'encoding': 'utf-
8', 'status': 'OK', 'address': '1037 State St, Hood River, Oregon, 97031', 'status_code': 200, 'confidence': 9}, 'bbox': [
-121.52281774656507, 45.706876183969186, -121.52081774656506, 45.70887618396918]}

How can I iterate through this structure and print it out nicely? 如何遍历此结构并很好地打印出来?

Is your goal, only to the print the structure or to parse it as well? 您的目标是仅打印结构还是对其进行解析?

In case you want to just print your output nicely, try this 如果您只想很好地打印输出,请尝试此操作

from pprint import pprint
pprint(d)

This shall provide you with a nicely printed structure. 这将为您提供印刷精美的结构。 In order to parse this, you can do it as you would with any dictionary object using keys and values. 为了对此进行解析,您可以像使用键和值对任何字典对象一样进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM