gpt4 book ai didi

python - 如何使用 python 从 json 文件打印特定值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:58 26 4
gpt4 key购买 nike

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

{
"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
}
}

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

到目前为止我有这段代码:

import json

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

最佳答案

试一试!

for key, value in json_data.iteritems():
result = value['result']

print result

更新(评论中的问题):如果您有多个文件并想一次存储所有信息 - 尝试将其全部放入字典中。这可能会有所不同,具体取决于您想要的 key 。但是试试这个(这将创建一个 {json_key: result_value} 的字典:

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

关于python - 如何使用 python 从 json 文件打印特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36970016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com