我有一个包含data
和meta data
的python文件。我在该文件上使用以下程序。
import requests
from lxml import html
import json
with open("tmp.json") as json_file:
json_data = json.load(json_file)
print(json_data)
在输出中,我在JSON文件中同时获得data
和metadata
。我想只提取数据部分。我该怎么做?
我的JSON文件的示例结构如下。
"data": [
{
"AvInterval": null,
"DelInterval": null,
"ntsGenerated": null,
"ntsGenerated": null,
"Metric": "Intervals",
"Total": 23
},
{
"AvInterval": null,
"DelInterval": null,
"ntsGenerated": null,
"ntsGenerated": null,
"Metric": "CPU",
"Total": 47
},
],
"metadata": {
"columns": [
{
"Caption": "Metric",
"Field": "Metric",
"Type": "string",
"Width": "*"
},
{
"Caption": "Total",
"Field": "Total",
"Type": "long",
"Width": "*"
},
]
}
我想从上述文件中删除metadata
并将data
剪切成不同的部分,并将每个单独的部分写入文件。
答案 0 :(得分:2)
如果您想将数据分成不同的部分并将每个部分写入自己的文件,您可以这样做:
with open("tmp.json") as json_file:
json_data = json.load(json_file)
for i, data_item in enumerate(json_data['data']):
fname = 'data_%s' % i
with open(fname, 'w') as outfile:
json.dump(data_item, outfile)
答案 1 :(得分:1)
使用data
键
json_data['data']
答案 2 :(得分:0)
假设下面是json,试试这个
import simplejson as json
with open("tmp.json", "r") as json_file:
extracted_data = json.loads(json_file)
for data in extracted_data['data']:
print data['AvInterval'], data['Metric'], data['DelInterval']
下面的json示例
{
"data": [
{
"AvInterval": null,
"DelInterval": null,
"ntsGenerated": null,
"ntsGenerated": null,
"Metric": "Intervals",
"Total": 23
},
{
"AvInterval": null,
"DelInterval": null,
"ntsGenerated": null,
"ntsGenerated": null,
"Metric": "CPU",
"Total": 47
}
],
"metadata": {
"columns": [
{
"Caption": "Metric",
"Field": "Metric",
"Type": "string",
"Width": "*"
},
{
"Caption": "Total",
"Field": "Total",
"Type": "long",
"Width": "*"
}
]
}
}