我正在尝试使用 pyparsing 解析日志文件。日志文件条目的结构如下:
information line 1
information line 2
...
information line n
attribute name 1 = attribute value 1
attribute name 2 = attribute value 2
...
attribute name n = attribute value n
我写了这个语法来解析这个文本块:
NL = Suppress(LineEnd())
infoline = OneOrMore(Word(alphas + ".-")) + NL
attrib_value_pair = OneOrMore(Word(alphas)) + Suppress("=") + \
OneOrMore(Word(alphanums + ".")) + NL
data = OneOrMore(infoline) + OneOrMore(attrib_value_pair)
parsed_data = data.parseString(trace_block)
解析时出现此错误:
Traceback (most recent call last):
File "filter.py", line 100, in <module>
get_trace_block()
File "filter.py", line 25, in get_trace_block
filter_trace(trace_block)
File "filter.py", line 53, in filter_trace
get_attrib_name_value_pairs(trace_block[2:len(trace_block)])
File "filter.py", line 94, in get_attrib_name_value_pairs
parsed = data.parseString(trace_block)
File "/usr/local/lib/python2.6/dist-packages/pyparsing.py", line 1006, in parseString
raise exc
pyparsing.ParseException: Expected end of line (at char 70), (line:2, col:26)
日志文件中的示例行:
A valid message has been received from a peer
Message type is A
local ip = 10.7.1.10
local port = 3000
remote ip = 10.7.1.20
remote port = 3001
知道哪里出错了吗?提前致谢。