繁体   English   中英

使用python从JSON对象中提取数据

[英]extracting data from JSON object with python

我正在尝试将数据从 JSON 对象中获取到 SAMPLES 和 LABELS 变量中,如下所示。

{
"samples": [
    [
        28,
        25,
        95
    ],
    [
        21,
        13,
        70
    ],
    [
        13,
        21,
        70
    ]
],
"labels": [
    1,
    2,
    3
  ]
 }

我正在使用的代码

with open(data, 'r') as d:
complete_data = json.load(d)
for a in complete_data:
    samples = a['samples']
    lables = a['lables']

但它说

样品 = a['样品']

类型错误:字符串索引必须是整数

要从'samples''labels'获取数据,您不需要使用循环。 尝试这个:

import json

with open('data.json', 'r') as d:
    complete_data = json.load(d)

samples = complete_data['samples']
labels = complete_data['labels']

print(samples)
print(labels)

输出:

[[28, 25, 95], [21, 13, 70], [13, 21, 70]]
[1, 2, 3]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM