我编写了一个python脚本,其中列出了AWS帐户中的所有AWS实例。它为您提供了公网IP和内网IP,以及一些其他信息。
但是当json中有多个嵌套的公网in和内网in时,会失败。我需要能够报告所有的公网和内网IP。
我是Python的新手。如果数据的内容是不可预测的,我需要知道如何构建数据字典。
例如,某些实例可能只有几个内网IP,也可能有10个或20个内网IP。公网IP也是如此。
数据是这样的:AWS Instance IPs
到目前为止,我的代码是这样的:
import boto3
import time
aws_account='company-prod'
session = boto3.Session(profile_name=aws_account)
ec2 = session.client("ec2")
instance_list = ec2.describe_instances()
for reservation in instance_list["Reservations"]:
for instance in reservation.get("Instances", []):
.... print out info about the instance....
我的脚本的完整代码可以在这里找到:AWS List Instances
如果数据因实例而异,我如何构建这些数据的字典?
发布于 2019-03-15 08:36:20
在Python中读/写.json
将数据读取到Python中:
with open('inputs.json', 'r') as file: # 'r' for read-only
my_variable = json.load(file)
将数据写入json:
with open('outputs.json', 'w') as f: # 'w' for write 'w+' will create a file if not found
json.dump(my_variable, f)
我想,当您正确加载.json文件时,标准Python索引将通过[]
很容易地访问它(这是用于索引列表、字典和元组的语法,但是在索引字典时,您应该放置一个键而不是索引->‘土豆’,而不是)。
https://stackoverflow.com/questions/55172214
复制相似问题