python - Python access nested Dictionary Value
问题描述
I'm converting JSON to a dictionary and then accessing values directly. Here's the JSON:
{
"timestamp": {
"format": "%Y-%m-%d %H:%M:%S"
},
"status": {
"grok_pattern": " status=%{NUMBER:status:int}"
},
"SQLite3_memory_bytes": {
"table": "stats_memory_metrics",
"saved_name": "sqlite3_memory_bytes"
},
"jemalloc_resident": {
"table": "stats_memory_metrics"
}
}
I can access single nested dictionaries fine but when I try to access a nested dictionary with multiple keys I get an error:
with open('/tmp/metrics.json') as json_file:
patterns = json.load(json_file)
print(patterns['jemalloc_resident']['table'])
print(patterns['timestamp']['format'])
print(patterns['SQLite3_memory_bytes']['saved_named'])
>> stats_memory_metrics
>> %Y-%m-%d %H:%M:%S
>> Traceback (most recent call last):
>> File "test.py", line 12, in <module>
print(patterns['SQLite3_memory_bytes']['saved_named'])
KeyError: 'saved_named'
How can I access the saved_named key's value?
解决方案
You made a typo. You put saved_named
instead of saved_name
.
推荐阅读
- html - CSS网格布局的严重问题
- opencl - OpenCL 数组返回 0
- python - 使用列表的单个元素扩展列表列表错误
- angular - 找不到“object”类型的不同支持对象“[object Object]”。NgFor 仅支持绑定到 Iterables,例如带有异步管道的数组
- c# - 单元测试操作顺序
- c# - 如何在 Visual Studio 中将 SSL 证书从 localhost 更改为其他内容
- xampp - 使用 XAMPP 的 Swiftmailer 问题
- python-3.x - 当另一列值与前一行不同时,如何将 pct 更改重置为 NaN?
- ruby-on-rails - 禁用模块/关注中关联的默认范围
- lua - PyCharm 上的 Love2d 控制台仅在关闭后写入控制台