我是Python新手。我想从监视中读取一个JSON文件。该文件如下所示
我只需要时间戳、系统名(如"test-1“)以及电源、磁盘和CPU的编号。
我的剧本看上去像这样
#!/usr/bin/env python3.3
import os
import json
with open('test.json', 'r') as f:
distros_dict = json.load(f)
for distro in distros_dict:
print(distro['"power [W]"'])更新:如何将它们写入变量?
新的样本数据:
[
{
"Area":"CLOUD",
"timestamp":"2019-11-06T00:00:00",
"Systeme":{
"test-4":{
"power [W]":181.05,
"disk [%]":52.28
},
"test-1":{
"power [W]":280.56,
"disk[%]":6.33,
"cpu[%]":0.1
},
"test-2":{
"power [W]":271.84,
"disk[%]":6.52,
"cpu[%]":0.1
},
"test-8":{
"power [W]":453.56,
"disk[%]":93.63,
"cpu[%]":5.04
}
}
}
]我想要的输出变量是cpu、power和磁盘,而不是将它们打印到命令行,而是将它们写入三个变量中。
发布于 2020-02-17 05:38:42
可以使用元组列表来存储变量。下面是你如何做到这一点的一个例子。
import json
import sys
data = None
with open('test.json', 'r') as file:
data = json.load(file)
# since we are dealing with input from another source, we should validate it every step of the way
# check if there was no data in the file
if not data:
sys.exit(0)
variables = [] # this list would contain tuples. The tuple would contain system name, cpu, power, and disk. Any missing data would be set as None
for distro in data:
if 'Systeme' not in distro: # we verify this attribute exists
continue
for system_name, system in distro['Systeme'].items():
variables.append(
(system_name, system.get('cpu[%]'), system.get('power [W]'), system.get('disk[%]'))
)
# now that we have them stored inside the 'variables' list, we can print them to verify that they are stored.
for system_name, cpu, power, disk in variables:
print(system_name, cpu, power, disk)
# do other stuff with these variables here if you want.发布于 2020-01-07 10:58:25
您的带有多个dict的json文件应该遵循以下格式:
[
{
"Area": "CLOUD",
"timestamp": "2019-11-06T12:24:25",
"Systeme": {
"test-1": {
"power [W]": 181.05,
"disk [%]": 52.28
},
"test-2": {
"power [W]": 199.67,
"disk [%]": 54.47
},
"test-3": {
"power [W]": 175.68,
"disk [%]": 10.17,
"cpu [%]": 22.43
}
}
},
{
"Area": "CLOUD",
"timestamp": "2019-11-06T12:24:25",
"Systeme": {
"test-1": {
"power [W]": 181.05,
"disk [%]": 52.28
},
"test-2": {
"power [W]": 199.67,
"disk [%]": 54.47
},
"test-3": {
"power [W]": 175.68,
"disk [%]": 10.17,
"cpu [%]": 22.43
}
}
}
]您只需将每个字典中的值组合成一个字符串即可得到如下结果:
with open('test.json', 'r') as f:
distros_dict = json.load(f)
test_keys = ['power [W]', 'disk [%]', 'cpu [%]']
for distro in distros_dict:
for key, value in distro['Systeme'].items():
output_list =[]
output_list.append(key)
for single_system_key, single_system_value in distro['Systeme'][key].items():
if single_system_key in test_keys:
output_list.append(single_system_value)
print(' - '.join(str(e) for e in output_list))最终输出得到:
test-1 - 181.05 - 52.28
test-2 - 199.67 - 54.47
test-3 - 175.68 - 10.17 - 22.43
test-1 - 181.05 - 52.28
test-2 - 199.67 - 54.47
test-3 - 175.68 - 10.17 - 22.43发布于 2020-01-07 11:13:37
设置您的字典(json格式,检查它是否在https://jsonlint.com/上使用正确的格式)
dict = {
"Area": "CLOUD",
"timestamp": "2019-11-06T12:24:25",
"Systeme": {
"test-1": {
"power [W]": 181.05,
"disk [%]": 52.28
},
"test-2": {
"power [W]": 199.67,
"disk [%]": 54.47
},
"test-3": {
"power [W]": 175.68,
"disk [%]": 10.17,
"cpu [%]": 22.43
}}}以json的形式加载字典
import json
x = json.loads(json.dumps(dict))循环遍历您的dictSysteme并获取您需要打印的所有属性。我选择使用dict.get("systeme")而不是dict["Systeme"],因为测试3中缺少cpu
for line in x["Systeme"]:
power = x.get("Systeme").get(line).get("power [W]", "")
disk = x.get("Systeme").get(line).get("disk [%]", "")
cpu = x.get("Systeme").get(line).get("cpu [%]", "")
print(f"{line} {power} {disk} {cpu}")
power, disk, cpu = "", "", ""注意:上面的示例是Python 3.7+,需要将print语句更改为Python的早期版本:
print("%s %s %s %s"%(line, power, disk, cpu) )
https://stackoverflow.com/questions/59626351
复制相似问题