我正在做一个Python项目,我试图与argparse
建立一些参数。当我在终端中输入其中一个时,我从JSON文件中获得一些信息,但我不知道如何做到这一点。到目前为止,我得到了这些东西:
{
"services": [
{
"name": "ac",
"version": "1.0.8",
"service": "running",
"url": "https://portal.azure.com/#home",
"disk usage": "20%"
},
{
"name": "acc",
"version": "1.0.8",
"service": "running",
"url": "https://portal.azure.com/#home",
"disk usage": "63%"
},
{
"name": "acv",
"version": "1.0.8",
"service": "running",
"url": "https://portal.azure.com/#home",
"disk usage": "37%"
},
{
"name": "acf",
"version": "1.0.8",
"service": "running",
"url": "https://portal.azure.com/#home",
"disk usage": "48%"
},
{
"name": "ach",
"version": "1.0.8",
"service": "error",
"url": "https://portal.azure.com/#home",
"disk usage": "10%"
},
{
"name": "acj",
"version": "1.0.8",
"service": "stopped",
"url": "https://portal.azure.com/#home",
"disk usage": "23%"
},
{
"name": "acq",
"version": "1.0.8",
"service": "running",
"url": "https://portal.azure.com/#home",
"disk usage": "65%"
},
{
"name": "bc",
"version": "1.0.8",
"service": "stopped",
"url": "https://portal.azure.com/#home",
"disk usage": "20%"
},
{
"name": "bcc",
"version": "1.0.8",
"service": "running",
"url": "https://portal.azure.com/#home",
"disk usage": "25%"
},
{
"name": "bcx",
"version": "1.0.8",
"service": "error",
"url": "https://portal.azure.com/#home",
"disk usage": "4%"
},
{
"name": "bcn",
"version": "1.0.8",
"service": "running",
"url": "https://portal.azure.com/#home",
"disk usage": "50%"
},
{
"name": "bcm",
"version": "1.0.8",
"service": "stopped",
"url": "https://portal.azure.com/#home",
"disk usage": "35%"
}
]
}
这是我的JSON文件这些是.py
中的参数:
#argparse parameters config
parser = argparse.ArgumentParser()
parser.add_argument("-f",
"--fullreport",
help="printing the full report",
default="*")
parser.add_argument("-g",
"--graphreport",
help="printing the graph report",
default="*")
parser.add_argument("-s",
"--services",
help="services to be test",
default=["*"])
parser.add_argument("-d",
"--diskusage",
help="see the disk usage")
args = parser.parse_args()
我有更多的代码,但它是打印所有的JSON文件与一个打开和一个for实例,但我想要的是打印例如,只是使用参数-s
的服务。
您可以使用for
循环:
def print_service_names(data):
print('Services:')
for service in data['services']:
print(service['name'])
def print_service_disk_usages(data):
print('Disk Usage:')
for service in data['services']:
print(f'{service["name"]}:t{service["disk usage"]}')
print_service_names(json_data)
print_service_disk_usages(json_data)
输出:Services:
ac
acc
acv
acf
ach
acj
acq
bc
bcc
bcx
bcn
bcm
Disk Usage:
ac: 20%
acc: 63%
acv: 37%
acf: 48%
ach: 10%
acj: 23%
acq: 65%
bc: 20%
bcc: 25%
bcx: 4%
bcn: 50%
bcm: 35%
如果你打算对数据做一些更高级的事情,你可以使用推导式来重塑它:
# format the data with comprehensions
service_names = [service['name']
for service in json_data['services']]
disk_usage = {service['name']: service['disk usage']
for service in json_data['services']}
from pprint import pprint
pprint(service_names)
pprint(disk_usage)
输出:['ac',
'acc',
'acv',
'acf',
'ach',
'acj',
'acq',
'bc',
'bcc',
'bcx',
'bcn',
'bcm']
{'ac': '20%',
'acc': '63%',
'acf': '48%',
'ach': '10%',
'acj': '23%',
'acq': '65%',
'acv': '37%',
'bc': '20%',
'bcc': '25%',
'bcm': '35%',
'bcn': '50%',
'bcx': '4%'}