Skip to main content
 首页 » 编程设计

python之使用 pyparsing 解析类似 json 的结构

2025年02月15日8tuyile006

我尝试使用 pyparsing 解析以下结构:

{ 
    firewall { 
        group { 
            address-group { 
                address 1.2.3.4 
            } 
            address-group { 
                address 5.6.7.4 
            } 
        } 
    } 
} 

代码:

from pyparsing import * 
 
LBRACE, RBRACE = map(Suppress, "{}") 
 
prop = Word(printables, excludeChars="{}\"") 
numberValue = pyparsing_common.number() 
 
configObject = Forward() 
value = Forward() 
value << (prop | numberValue | Group(configObject)) 
memberDef = Group(prop + value) 
configMembers = delimitedList(memberDef) 
configObject << Dict(LBRACE + Optional(configMembers) + RBRACE) 
 
if __name__ == "__main__": 
    testdata = """ 
    { 
        firewall { 
            group { 
                address-group { 
                    address 1.2.3.4 
                } 
                address-group { 
                    address 1.2.3.4 
                } 
            } 
        } 
    } 
    """ 
 
 
results = configObject.parseString(testdata)   
 
print(results.firewall.group) 

它抛出

pyparsing.ParseException: Expected "}" (at char 15), (line:3, col:9)

如果我只有一个 address-group 它正在工作:

{ 
    firewall { 
        group { 
            address-group { 
                address 1.2.3.4 
            } 
        } 
    } 
} 

编辑:

{ 
    firewall { 
        group { 
            address-group { 
                address 1.2.3.4 
            } 
            address-group { 
                address 5.6.7.4 
            } 
        } 
    } 
} 

使用 configMembers = OneOrMore(memberDef) 而不是 configMembers = delimitedList(memberDef) 来解决 我还需要解析它(注意 adress-group 之后的 name:

{ 
    firewall { 
        group { 
            address-group name1 { 
                address 1.2.3.4 
            } 
            address-group name2 { 
                address 5.6.7.4 
            } 
        } 
    } 
} 

请您参考如下方法:

我在两个地址组之间添加了一个逗号,一切正常

{ 
firewall { 
    group { 
        address-group { 
            address 1.2.3.4 
        }, 
        address-group { 
            address 5.6.7.4 
        } 
    } 
} 
} 

因此代码是

from pyparsing import * 
 
LBRACE, RBRACE = map(Suppress, "{}") 
 
prop = Word(printables, excludeChars="{}\"") 
numberValue = pyparsing_common.number() 
 
configObject = Forward() 
value = Forward() 
value << (prop | numberValue | Group(configObject)) 
memberDef = Group(prop + value) 
configMembers = delimitedList(memberDef) 
configObject << Dict(LBRACE + Optional(configMembers) + RBRACE) 
 
if __name__ == "__main__": 
    testdata = """ 
    { 
        firewall { 
            group { 
                address-group { 
                    address 1.2.3.4 
                }, 
                address-group { 
                    address 5.6.7.4 
                } 
            } 
        } 
    } 
    """ 
 
 
results = configObject.parseString(testdata) 
 
print(results.firewall.group) 

输出是

[['address-group', [['address', '1.2.3.4']]], ['address-group', [['address',  
'5.6.7.4']]]]