[英]JQ to Python Conversion
I can pull a list of AWS EC2 regions using the aws cli with this JQ command: 我可以通过以下JQ命令使用aws cli拉出AWS EC2区域的列表:
aws ec2 describe-regions --profile=company-nonprod | jq -r '.Regions[].RegionName'
That gives me the following list: 这给了我以下列表:
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
How can I do the same thing in python? 我如何在python中做同样的事情?
I tried: 我试过了:
import boto3
aws_regions = ec2.describe_regions()
for region in aws_regions:
print("AWS Regions: ", aws_regions, '\n')
print("Region: ", region)
I include the region json in the output and the value of 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
But I don't know how to access the values in the resulting json above. 但是我不知道如何访问上面生成的json中的值。
ec2.describe_regions()
returns a Python dict
. ec2.describe_regions()
返回Python dict
。 If you simply loop over it, you will get its keys. 如果仅在其上循环,则将获得其密钥。 That's what you're doing. 那就是你在做什么。
You can access the value corresponding to the key Regions
by writing this: 您可以通过编写以下代码来访问与键Regions
对应的值:
# equivalent of JQ '.Regions'
aws_regions['Regions']
Then you can loop over it: 然后,您可以遍历它:
# equivalent of JQ '.Regions[]'
for region in aws_regions['Regions']:
...
You will get a dict
again for each region. 您将再次获得每个区域的dict
。
Then you can read the value corresponding to the key RegionName
: 然后,您可以读取与键RegionName
相对应的值:
# equivalent of JQ '.Regions[].RegionName'
name = region['RegionName']
Finally, you can wrap all this to something like this: 最后,您可以将所有这些包装成如下形式:
import boto3
aws_regions = ec2.describe_regions()
for region in aws_regions['Regions']:
print(region['RegionName'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.