Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 条件解析JSON文件_Python_Json - Fatal编程技术网

Python 条件解析JSON文件

Python 条件解析JSON文件,python,json,Python,Json,我想在Tags[]中有值时解析这个JSON,否则就为Tag打印No Tag行 我有以下代码: { "RouteTables": [ { "Associations": [], "RouteTableId": "rtb-ce3c7daa", "VpcId": "vpc-87cf4de3", "PropagatingVgws": [], "Tags"

我想在
Tags[]
中有值时解析这个JSON,否则就为Tag打印
No Tag

我有以下代码:

{
    "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"
                }
            ]
        }
无论是否有标签,它都会打印
无标签

期望输出:

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
目前的结果:

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

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

尝试在代码中替换这一行:

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
作者:


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

if element['Tags']:

对于缩进块,可以使用四个空格。单字符缩进很难理解。您的
else
应该链接到您的
for
吗?或者这是一个错误吗?
如果需要检查
标记是否存在,如果不存在,则分配d='No Tag',并以这种方式进一步执行,没有标记线written@Milister在您的代码中,每个元素都有“Tags”键,但问题在于您的条件始终返回false。因此,一旦您尝试了上述代码,我认为它是有效的。@Milister Welcome:-)如果有任何缩进错误,代码将不会执行。它在运行时显示缩进错误。
if element['Tags']:
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