【问题标题】:Iterating over a dict object that contains nested elements迭代包含嵌套元素的 dict 对象
【发布时间】:2017-07-29 05:12:39
【问题描述】:

Python 5.6

这是使用地理编码器模块调用的结果

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]}

我怎样才能遍历这个结构并很好地打印出来?

【问题讨论】:

  • from pprint import pprint; pprint(d)
  • 5.6??也许是 3.6?

标签: python dictionary iteration


【解决方案1】:

您的目标是仅打印结构还是解析结构?

如果您只想很好地打印输出,试试这个

from pprint import pprint
pprint(d)

这将为您提供一个印刷精美的结构。 为了解析它,您可以像使用任何字典对象一样使用键和值来解析它。

【讨论】:

  • 这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 2013-05-06
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2017-06-11
相关资源
最近更新 更多