首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用python从json文件中打印特定的值?

如何使用python从json文件中打印特定的值?
EN

Stack Overflow用户
提问于 2016-05-01 17:58:18
回答 4查看 6.2K关注 0票数 1

我是使用python的新手,我想知道如何使用python从json文件中打印一些值,下面是我的json文件:

代码语言:javascript
运行
复制
{
    "igt@gem_reloc_overflow@single-overflow": {
        "__type__": "TestResult",
        "command": "/home/gfx/intel-graphics/intel-gpu-tools/tests/gem_reloc_overflow --run-subtest single-overflow",
        "dmesg": "",
        "environment": "PIGLIT_PLATFORM=\"mixed_glx_egl\" PIGLIT_SOURCE_DIR=\"/home/gfx/intel-graphics/intel-gpu-tools/piglit\"",
        "err": "(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: __gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\nSubtest single-overflow failed.\n**** DEBUG ****\n(gem_reloc_overflow:19562) DEBUG: relocation_count=4294967295\n(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: __gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\n****  END  ****\n",
        "exception": null,
        "out": "IGT-Version: 1.14-g1e9a3ac (x86_64) (Linux: 4.6.0-rc4-drm-intel-nightly-ww17-commit-1e81bac+ x86_64)\nStack trace:\n  #0 [__igt_fail_assert+0x101]\n  #1 [reloc_tests+0x6d6]\n  #2 [<unknown>+0x6d6]\nSubtest single-overflow: FAIL (8.469s)\n",
        "pid": 19562,
        "result": "fail",
        "returncode": 99,
        "subtests": {
            "__type__": "Subtests"
        },
        "time": {
            "__type__": "TimeAttribute",
            "end": 1462072402.5360818,
            "start": 1462072393.7328644
        },
        "traceback": null
    }
}

我需要的价值是“结果:失败”。

到目前为止,我有这样的代码:

代码语言:javascript
运行
复制
import json

with open("9.json") as json_file:
json_data = json.load(json_file)
print(json_data)
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-05-01 18:02:27

试试看!

代码语言:javascript
运行
复制
for key, value in json_data.iteritems():
    result = value['result']

print result

更新(在注释中提问):如果您有多个文件,并且希望一次存储所有信息,请尝试将其全部放入字典中。这可以根据您想要的密钥而有所不同。但是尝试这个(这将创建一个{json_key: result_value}的副分支

代码语言:javascript
运行
复制
all_results = {}
json_file_list = ['file_1.json', 'file_2.json']
for file in json_file_list:
    with open(file) as json_file:
        json_data = json.load(json_file)
        for key, value in json_data.iteritems():
            if 'result' in value:
                all_results[key] = value['result']
return all_results
票数 1
EN

Stack Overflow用户

发布于 2016-05-01 18:02:14

json.load函数返回一个字典(一个dict类型的对象)。

字典把键和vlues联系起来。要访问字典中的值,可以使用以下语法:

代码语言:javascript
运行
复制
value = dictionary[key]

就你的具体情况而言:

代码语言:javascript
运行
复制
result = json_data['result']
票数 1
EN

Stack Overflow用户

发布于 2016-05-01 18:02:15

代码语言:javascript
运行
复制
json_data["igt@gem_reloc_overflow@single-overflow"]["result"]

这将打印结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36970016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档