python - Python字典重构
问题描述
我正在构建一个 Flask REST API 来向 Google Places API 发出请求。
我的麻烦是以 Pythonic 方式处理嵌套的字典。
来自 Google Places 的(缩写)响应类似于:
[
{
"name": <string>,
"opening_hours": { "open_now": <bool> },
"photos": [{ "height": <string>, "width": <string>, "photo_reference": <string> }],
"rating": <number>,
}
]
我想将此 dict 重组为类似于:
[
{
"name": <string> OR '',
"opening_hours": <bool> OR false,
"photoSrc": <string> # (built from the h, w, and photo_reference keys),
"rating": <number> OR None
}
]
在 JavaScript 中,我通常会做一些事情,例如使用默认值进行嵌套解构或为功能组合创建辅助函数,例如pathOr(['path', 'to', 'key'], defaultValue)
我想知道其他人通常如何在他们的 Python API 中处理这样的事情。
提前十亿谢谢!
解决方案
推荐阅读
- vba - 如何使 .MergeShapes 行为正常?
- java - 如何在 Java 中读取/获取表情符号的 unicode 值
- swift - 通用 NSKeyValueObservedChange/Keypath
- youtube-api - 如何使用 YouTube API 将 YouTube 播放列表设置为“官方系列播放列表”?
- django - 无法在 Windows 10 上运行 django-admin startproject mysite
- java - JavaFX 程序通过 IDE 运行,但不是独立运行(Ubuntu)
- xamarin.forms - 强制呈现 HTML
- sql - 从列中提取字符串以创建新列
- ruby - Ruby 2.6.3 在 Mac 上安装失败
- javascript - 使用 noUiSlider 更新与显示格式不同的隐藏输入