python - attribute error when using format - python
问题描述
D = {'z': {'A': 'Hello', 'B': 'World'}}
'{z.get("A")} {z.get("B")}'.format(**D)
*** AttributeError: 'dict' object has no attribute 'get("A")'
Why am I getting this error, and how should I fix it?
I'd like to print "Hello World"
解决方案
您可以在python3
D = {'z': {'A': 'Hello', 'B': 'World'}}
print(f"{D['z'].get('A')} {D['z'].get('B')}")
或者你可以试试这个
print(f"{D['z']['A']} {D['z']['B']}")
输出:
Hello World
推荐阅读
- c++ - 使用 c++ 代码中的参数调用 masm 函数
- ios - 如何处理uitableview上的已读和未读通知消息
- swift - 如何将 JSON 响应解析为模型并加载到 tableview 中?
- javascript - 页面重新加载时保持布局
- android - 在 RecyclerView 的底部禁用过度滚动效果
- node.js - 在 alpine 上从源代码安装 node.js
- clojure - 在 Clojure 中创建 Uberjar 的问题
- jsp - 会话传递给 jsp
- haskell - 找不到 filename.hs 的底座。继续使用隐式摇篮
- javascript - JQuery SmartMenu, SelectMenu onclick 切换 json 数据