如何解析代码(Python)?
我需要解析一些特殊的数据结构。它们的格式有点像C语言,差不多是这样的:
Group("GroupName") {
/* C-Style comment */
Group("AnotherGroupName") {
Entry("some","variables",0,3.141);
Entry("other","variables",1,2.718);
}
Entry("linebreaks",
"allowed",
3,
1.414
);
}
我想到几种处理这个问题的方法。我可以用正则表达式把代码“切分”成小块。我也可以一个字符一个字符地读取代码,然后用状态机来构建我的数据结构。还可以去掉逗号和换行符,逐行读取内容。或者我可以写一个转换脚本,把这些代码转换成可以执行的Python代码。
有没有什么好的Python方法来解析这样的文件呢?
你会怎么解析它呢?
这个问题更一般,主要是关于如何解析字符串,而不是特定的文件格式。
3 个回答
7
使用pyparsing这个库(Mark Tolonen,我正要点击“提交帖子”时,你的帖子刚好发过来),这其实很简单——请看下面代码中的注释:
data = """Group("GroupName") {
/* C-Style comment */
Group("AnotherGroupName") {
Entry("some","variables",0,3.141);
Entry("other","variables",1,2.718);
}
Entry("linebreaks",
"allowed",
3,
1.414
);
} """
from pyparsing import *
# define basic punctuation and data types
LBRACE,RBRACE,LPAREN,RPAREN,SEMI = map(Suppress,"{}();")
GROUP = Keyword("Group")
ENTRY = Keyword("Entry")
# use parse actions to do parse-time conversion of values
real = Regex(r"[+-]?\d+\.\d*").setParseAction(lambda t:float(t[0]))
integer = Regex(r"[+-]?\d+").setParseAction(lambda t:int(t[0]))
# parses a string enclosed in quotes, but strips off the quotes at parse time
string = QuotedString('"')
# define structure expressions
value = string | real | integer
entry = Group(ENTRY + LPAREN + Group(Optional(delimitedList(value)))) + RPAREN + SEMI
# since Groups can contain Groups, need to use a Forward to define recursive expression
group = Forward()
group << Group(GROUP + LPAREN + string("name") + RPAREN +
LBRACE + Group(ZeroOrMore(group | entry))("body") + RBRACE)
# ignore C style comments wherever they occur
group.ignore(cStyleComment)
# parse the sample text
result = group.parseString(data)
# print out the tokens as a nice indented list using pprint
from pprint import pprint
pprint(result.asList())
输出结果是
[['Group',
'GroupName',
[['Group',
'AnotherGroupName',
[['Entry', ['some', 'variables', 0, 3.141]],
['Entry', ['other', 'variables', 1, 2.718]]]],
['Entry', ['linebreaks', 'allowed', 3, 1.4139999999999999]]]]]
(不幸的是,可能会有些混淆,因为pyparsing定义了一个“Group”类,用来给解析出来的内容加上结构——注意在一个Entry中,值的列表是如何被分组的,因为列表表达式被包裹在一个pyparsing的Group里。)