条件解析JSON-fi

2024-09-30 18:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

{
    "RouteTables": [
        {
            "Associations": [], 
            "RouteTableId": "rtb-ce3c7daa", 
            "VpcId": "vpc-87cf4de3", 
            "PropagatingVgws": [], 
            "Tags": [
                {
                    "Value": "ItMgmtUsEastPublic", 
                    "Key": "Name"
                }
            ], 
            "Routes": [
                {
                    "GatewayId": "local", 
                    "DestinationCidrBlock": "192.168.254.0/23", 
                    "State": "active", 
                    "Origin": "CreateRouteTable"
                }, 
                {
                    "GatewayId": "igw-961518f3", 
                    "DestinationCidrBlock": "0.0.0.0/0", 
                    "State": "active", 
                    "Origin": "CreateRoute"
                }
            ]
        }, 
        {
            "Associations": [
                {
                    "RouteTableAssociationId": "rtbassoc-27e68942", 
                    "Main": true, 
                    "RouteTableId": "rtb-92ff64f7"
                }
            ], 
            "RouteTableId": "rtb-92ff64f7", 
            "VpcId": "vpc-b8fc75dd", 
            "PropagatingVgws": [], 
            "Tags": [], 
            "Routes": [
                {
                    "GatewayId": "local", 
                    "DestinationCidrBlock": "172.31.0.0/16", 
                    "State": "active", 
                    "Origin": "CreateRouteTable"
                }, 
                {
                    "GatewayId": "igw-27cd1542", 
                    "DestinationCidrBlock": "0.0.0.0/0", 
                    "State": "active", 
                    "Origin": "CreateRoute"
                }
            ]
        }

我想在Tags[]中有值的情况下解析这个JSON写它,否则打印No Tag行作为标记

我有这个密码:

with open('1.json') as file:
 data = json.load(file)

 for element in data['RouteTables']:
  s=element['RouteTableId'] + ',' + element['VpcId']
  if 'Tags' in element:
   for route in element['Tags']:
    d = route['Value']
   else:
    d = 'No Tag'
  for route in element['Routes']:
     r=route['DestinationCidrBlock']
     print s+','+d+','+r

不管是否有标记,它都打印No Tag

期望输出:

rtb-ce3c7daa,vpc-87cf4de3,ItMgmtUsEastPublic,192.168.254.0/23
rtb-ce3c7daa,vpc-87cf4de3,ItMgmtUsEastPublic,0.0.0.0/0
rtb-92ff64f7,vpc-b8fc75dd,No Tag,172.31.0.0/16
rtb-92ff64f7,vpc-b8fc75dd,No Tag,0.0.0.0/0

当前结果:

rtb-ce3c7daa,vpc-87cf4de3,No Tag,192.168.254.0/23
rtb-ce3c7daa,vpc-87cf4de3,No Tag,0.0.0.0/0
rtb-92ff64f7,vpc-b8fc75dd,No Tag,172.31.0.0/16
rtb-92ff64f7,vpc-b8fc75dd,No Tag,0.0.0.0/0

Tags: nointagtagselementoriginvpcactive
3条回答

修正了你的代码。基本上,每次迭代都需要重新初始化

for element in data['RouteTables']:
    s=element['RouteTableId'] + ',' + element['VpcId']
    d = 'No Tag'
    if 'Tags' in element:
        for route in element['Tags']:
            d = route['Value']
    for route in element['Routes']:
        r=route['DestinationCidrBlock']
        print s+','+d+','+r

尝试替换代码中的此行:

if 'Tags' in element:  

签署人:

if element['Tags']:

我想你把“else”的缩进弄错了。这样你就有了for…else,而不是if…else

相关问题 更多 >