我试图获得在EC2中创建的网络接口,并且由于它,我正在使用" descripbe_network_interfaces& quot;函数从boto3。这个函数的输出是一个像这样的结构体:
{
'NetworkInterfaces': [
{
'Association': {
'AllocationId': 'string',
'AssociationId': 'string',
'IpOwnerId': 'string',
'PublicDnsName': 'string',
'PublicIp': 'string',
'CustomerOwnedIp': 'string',
'CarrierIp': 'string'
},
'Attachment': {
'AttachTime': datetime(2015, 1, 1),
'AttachmentId': 'string',
'DeleteOnTermination': True|False,
'DeviceIndex': 123,
'NetworkCardIndex': 123,
'InstanceId': 'string',
'InstanceOwnerId': 'string',
'Status': 'attaching'|'attached'|'detaching'|'detached',
'EnaSrdSpecification': {
'EnaSrdEnabled': True|False,
'EnaSrdUdpSpecification': {
'EnaSrdUdpEnabled': True|False
}
}
},
'AvailabilityZone': 'string',
'Description': 'string',
'Groups': [
{
'GroupName': 'string',
'GroupId': 'string'
},
],
'InterfaceType': 'interface'|'natGateway'|'efa'|'trunk'|'load_balancer'|'network_load_balancer'|'vpc_endpoint'|'branch'|'transit_gateway'|'lambda'|'quicksight'|'global_accelerator_managed'|'api_gateway_managed'|'gateway_load_balancer'|'gateway_load_balancer_endpoint'|'iot_rules_managed'|'aws_codestar_connections_managed',
'Ipv6Addresses': [
{
'Ipv6Address': 'string'
},
],
'MacAddress': 'string',
'NetworkInterfaceId': 'string',
'OutpostArn': 'string',
'OwnerId': 'string',
'PrivateDnsName': 'string',
'PrivateIpAddress': 'string',
'PrivateIpAddresses': [
{
'Association': {
'AllocationId': 'string',
'AssociationId': 'string',
'IpOwnerId': 'string',
'PublicDnsName': 'string',
'PublicIp': 'string',
'CustomerOwnedIp': 'string',
'CarrierIp': 'string'
},
'Primary': True|False,
'PrivateDnsName': 'string',
'PrivateIpAddress': 'string'
},
],
'Ipv4Prefixes': [
{
'Ipv4Prefix': 'string'
},
],
'Ipv6Prefixes': [
{
'Ipv6Prefix': 'string'
},
],
'RequesterId': 'string',
'RequesterManaged': True|False,
'SourceDestCheck': True|False,
'Status': 'available'|'associated'|'attaching'|'in-use'|'detaching',
'SubnetId': 'string',
'TagSet': [
{
'Key': 'string',
'Value': 'string'
},
],
'VpcId': 'string',
'DenyAllIgwTraffic': True|False,
'Ipv6Native': True|False,
'Ipv6Address': 'string'
},
],
'NextToken': 'string'
}
我怎样才能从"NetworkInterfaceId"把它列在清单上?我试图提取这个值使用正则表达式,但我还没有很好的技能。你们能帮帮我吗?
要从字典中获取值,必须使用" navigate" "到它。例如:
my_network_interface_id = my_dict["NetworkInterfaces"][0]["NetworkInterfaceId"]
我们需要0来获取数组的第一个元素。你可能也想看看@JonSG的回答。
可以使用for的缩写
network_interfaces = [interface["NetworkInterfaceId"] for interface in response["NetworkInterfaces"]]
mydict['NetworkInterfaces'][0]['NetworkInterfaceId']