python - 用 .format 替换 dict 中存在的所有参数(所有字符串)
问题描述
I would like to know if there is any pythonic way of replacing all arguments (of all strings) that exist in a dictionary with .format. For example:
dictionary = {"name": "{name_user}", "age": 26, "infos": [ {"name": "{name_user}" }]}
information = {"name_user": "Joao"}
Different from the following:
dictionary["name"] = dictionary["name"].format(**information)
dictionary["infos"][0]["name"] = dictionary["infos"][0]["name"].format(**information)
I wonder if there is any more efficient way to do this.
Note: the dictionary would be a JSON file
解决方案
这是另一种方法
information = {"name_user": "Joao"}
dictionary = {
"name": information.get("name_user", ""),
"age": 26,
"infos": [
{
"name": information.get("name_user", "")
}]
}
我不知道这是否是最有效的方法,但我认为是更简单的方法。
推荐阅读
- python - APPIUM:无法绑定监听器:无法绑定到 127.0.0.1:8200
- c++ - C++ - 如何比使用 unordered_map 更快地缓冲计算结果
- python - Plotly - 如何在悬停时突出显示节点的路径?
- type-inference - Protege 中的推断不会基于 `subClassOf` 推断类型
- python - 在熊猫中并行化 groupby 和 agg 的有效方法
- jupyter-notebook - 用户登录时无法将 JupyterHub helm chart 配置为使用 github 身份验证?
- c++ - 如何从 vcpkg 导入 cmake 中的包?
- android - 数据绑定如何与 Android 中的 DataBindingUtil.bind 与 ActivityBinding.inflate 一起使用
- rust - 如何分配给切片范围?
- node.js - 使用 babylonjs 在 nodejs 上验证 gltf/glb 文件