使用 pyparsing,处理这样的结构化文本格式并不难。
下面是解析器的样子:
from pyparsing import (Suppress, Keyword, Word, alphas, alphanums, Combine,
OneOrMore, quotedString, removeQuotes, Group, ZeroOrMore)
LBRACE,RBRACE,EQ = map(Suppress, "{}=")
OBJECT = Keyword("object")
IMPORT = Keyword("import")
ident = Word(alphas, alphanums)
dottedIdent = Combine(ident + OneOrMore("." + ident))
quotedString.setParseAction(removeQuotes)
propertyDefn = Group((dottedIdent | ident)("name") + EQ + quotedString("value"))
importDirective = Group(IMPORT + quotedString('source'))
objectBodyDefn = Group(ZeroOrMore(propertyDefn("properties*") |
importDirective("imports*")))
objectDefn = Group(OBJECT + ident("type") + quotedString("name") +
LBRACE + objectBodyDefn("body") + RBRACE)
parser = ZeroOrMore(objectDefn)
以下是应用解析器和访问解析数据的方法:
# parsing the sample, and accessing the parsed data fields
for obj in parser.parseString(sample):
print(obj.dump())
print("%(name)s (%(type)s)" % obj)
print("imports:", ','.join(imp.source for imp in obj.body.imports))
print("properties:")
if obj.body.properties:
for prop in obj.body.properties:
print('-', prop.name, ':', prop.value)
else:
print(' ','<none>')
print()
有了这个输出:
['object', 'Host', 'server1', [['import', 'generic-host'], ['address', '192.168.0.1'], ['vars.os', 'Linux']]]
- body: [['import', 'generic-host'], ['address', '192.168.0.1'], ['vars.os', 'Linux']]
- imports:
[0]:
['import', 'generic-host']
- source: generic-host
- properties:
[0]:
['address', '192.168.0.1']
- name: address
- value: 192.168.0.1
[1]:
['vars.os', 'Linux']
- name: vars.os
- value: Linux
- name: server1
- type: Host
server1 (Host)
imports: generic-host
properties:
- address : 192.168.0.1
- vars.os : Linux
['object', 'Host', 'server2', [['import', 'generic-host'], ['address', '192.168.0.2'], ['vars.os', 'Linux']]]
- body: [['import', 'generic-host'], ['address', '192.168.0.2'], ['vars.os', 'Linux']]
- imports:
[0]:
['import', 'generic-host']
- source: generic-host
- properties:
[0]:
['address', '192.168.0.2']
- name: address
- value: 192.168.0.2
[1]:
['vars.os', 'Linux']
- name: vars.os
- value: Linux
- name: server2
- type: Host
server2 (Host)
imports: generic-host
properties:
- address : 192.168.0.2
- vars.os : Linux