python - 类型错误 : string indices must be integers when I try and get values from JSON

标签 python json python-3.x

我正在尝试从这些数据中获取特定值,

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/

相关文章:

python - 无法使用 del 函数删除 json 中的字典键。收到类型错误

python - 有没有办法读取 dataframe.to_json(orient ='table') 保存的 JSON 文件?

python - 通过函数传递变量/列表

python - 通过匹配另一个数据帧的多级标题来填充数据帧的列

python - 将 pandas 数据框与每个索引的不同行连接起来

python - 如何在python中找到内置函数的复杂性

python - 如何使用 beautifulsoup 提取 onClick url

javascript - 如何将数据从 JSONP 转换为 JSON

design-patterns - Python 设计模式,交叉导入

python - Python 中的消息框