如何将 argparse 与 python 中的 json 个文件一起使用

How to use argparse with json files in python

我正在 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 文件,带有一个 with open 和一个 for instances 但我想要的是打印例如使用参数 -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%'}