我正在尝试从这些数据中获取特定值,
data = {'Memory': [{'SensorType': 'Load', 'Value': 51.9246254}], 'CPU Core #2': [{'SensorType': 'Temperature', 'Value': 63}, {'SensorType': 'Load', 'Value': 66.40625}, {'SensorType': 'Clock', 'Value': 2700.006}]}
我正在尝试使用以下方法从内存
获取SensorType
,
print(data["Memory"]["SensorType"])
但是我收到此错误,
TypeError: string indices must be integers
关于为什么会发生这种情况有什么想法吗?
最佳答案
data
的键 Memory
的值是一个 list
(一个元素:一个 dict
),不是字典
。因此,您还需要获取 list
的唯一元素:
data["Memory"][0]["SensorType"]
<小时/>
请注意,对于您的示例,您应该收到错误:
TypeError: list indices must be integers or slices, not str
不是您发布的。我认为错误消息只是错误地放置。
关于python - 类型错误 : string indices must be integers when I try and get values from JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51736114/