使用 python 从 JSON 文件中提取部分数据
Extract part of data from JSON file using python
我一直在尝试使用 python.I 从 JSON 文件中仅提取某些数据,想从我的 json 创建一个数组,其中有一些条目如下
[{"Device Name":"abc", "Device Caps":["a1", "b1", "c1"]},
{"Device Name":"def", "Device Caps":["a2", "b2", "c2"]},
{"Device Name":"ghi", "Device Caps":["a3", "b3", "c3"]},
{"Device Name":"jkl", "Device Caps":["a4", "b4", "c4"]}]
我需要我的输出作为
["a1","a2","a3","a4"]
如果这确实是您的输入文件,那么以下代码将产生您想要的输出:
import json
with open("input.json") as input_file:
data = json.load(input_file)
data = [d["Device Caps"][0] for d in data]
print(data)
我一直在尝试使用 python.I 从 JSON 文件中仅提取某些数据,想从我的 json 创建一个数组,其中有一些条目如下
[{"Device Name":"abc", "Device Caps":["a1", "b1", "c1"]},
{"Device Name":"def", "Device Caps":["a2", "b2", "c2"]},
{"Device Name":"ghi", "Device Caps":["a3", "b3", "c3"]},
{"Device Name":"jkl", "Device Caps":["a4", "b4", "c4"]}]
我需要我的输出作为 ["a1","a2","a3","a4"]
如果这确实是您的输入文件,那么以下代码将产生您想要的输出:
import json
with open("input.json") as input_file:
data = json.load(input_file)
data = [d["Device Caps"][0] for d in data]
print(data)