JQ到Python的转换

时间:2019-03-11 16:26:17

标签: python

我可以通过以下JQ命令使用aws cli拉出AWS EC2区域列表:

aws ec2 describe-regions --profile=company-nonprod | jq -r '.Regions[].RegionName'

这给了我以下列表:

eu-north-1
ap-south-1
eu-west-3
eu-west-2
eu-west-1
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2

如何在python中做同样的事情?

我尝试过:

import boto3
aws_regions = ec2.describe_regions()
for region in aws_regions:
    print("AWS Regions: ", aws_regions, '\n')
    print("Region: ", region)

我在输出中包含region json和region的值:

AWS Regions:  {'Regions': [{'Endpoint': 'ec2.eu-north-1.amazonaws.com', 'RegionName': 'eu-north-1'}, {'Endpoint': 'ec2.ap-south-1.amazonaws.com', 'RegionName': 'ap-south-1'}, {'Endpoint': 'ec2.eu-west-3.amazonaws.com', 'RegionName': 'eu-west-3'}, {'Endpoint': 'ec2.eu-west-2.amazonaws.com', 'RegionName': 'eu-west-2'}, {'Endpoint': 'ec2.eu-west-1.amazonaws.com', 'RegionName': 'eu-west-1'}, {'Endpoint': 'ec2.ap-northeast-2.amazonaws.com', 'RegionName': 'ap-northeast-2'}, {'Endpoint': 'ec2.ap-northeast-1.amazonaws.com', 'RegionName': 'ap-northeast-1'}, {'Endpoint': 'ec2.sa-east-1.amazonaws.com', 'RegionName': 'sa-east-1'}, {'Endpoint': 'ec2.ca-central-1.amazonaws.com', 'RegionName': 'ca-central-1'}, {'Endpoint': 'ec2.ap-southeast-1.amazonaws.com', 'RegionName': 'ap-southeast-1'}, {'Endpoint': 'ec2.ap-southeast-2.amazonaws.com', 'RegionName': 'ap-southeast-2'}, {'Endpoint': 'ec2.eu-central-1.amazonaws.com', 'RegionName': 'eu-central-1'}, {'Endpoint': 'ec2.us-east-1.amazonaws.com', 'RegionName': 'us-east-1'}, {'Endpoint': 'ec2.us-east-2.amazonaws.com', 'RegionName': 'us-east-2'}, {'Endpoint': 'ec2.us-west-1.amazonaws.com', 'RegionName': 'us-west-1'}, {'Endpoint': 'ec2.us-west-2.amazonaws.com', 'RegionName': 'us-west-2'}], 'ResponseMetadata': {'RequestId': 'cdd8b735-1705-44b7-819c-a58196d77ea8', 'HTTPStatusCode': 200, 'HTTPHeaders': {'content-type': 'text/xml;charset=UTF-8', 'content-length': '2711', 'vary': 'Accept-Encoding', 'date': 'Mon, 11 Mar 2019 16:18:00 GMT', 'server': 'AmazonEC2', 'cache-control': 'proxy-revalidate', 'connection': 'Keep-Alive'}, 'RetryAttempts': 0}}

Region:  Regions

但是我不知道如何访问上面生成的json中的值。

0 个答案:

没有答案