Matching nonempty lines with pyparsing

荒凉一梦 提交于 2019-12-04 03:51:23

问题


I am trying to make a small application which uses pyparsing to extract data from files produced by another program.

These files have following format.

SOME_KEYWORD:
line 1
line 2
line 3
line 4

ANOTHER_KEYWORD:
line a
line b
line c

How can i construct grammar which will help to extract line 1, line 2 ... line 4 and line a .. line c? I am trying to make a construction like this

Grammar = Keyword("SOME_KEYWORD:").supress() + NonEmptyLines + EmptyLine.supress() +\
         Keyword("ANOTHER_KEYWORD:").supress() + NonEmptyLines + EmptyLine.supress()

But i don't know how to define NonEmptyLines and EmptyLine. Thanks.


回答1:


My take on it:

    from pyparsing import *

    # matches and removes end of line
    EOL = LineEnd().suppress()

    # line starts, anything follows until EOL, fails on blank lines,
    line = LineStart() + SkipTo(LineEnd(), failOn=LineStart()+LineEnd()) + EOL

    lines = OneOrMore(line)

    # Group keyword probably helps grouping these items together, you can remove it
    parser = Keyword("SOME_KEYWORD:") + EOL + Group(lines) + Keyword("ANOTHER_KEYWORD:") + EOL + Group(lines)
    result = parser.parseFile('data.txt')
    print result

Result is:

['SOME_KEYWORD:', ['line 1', 'line 2', 'line 3', 'line 4'], 'ANOTHER_KEYWORD:', ['line a', 'line b', 'line c']]



回答2:


This takes you most of the way there:

import pyparsing as pp

data = """
SOME_KEYWORD:
line 1
line 2
line 3
line 4

ANOTHER_KEYWORD:
line a
line b
line c
"""

some_kw = pp.Keyword('SOME_KEYWORD:').suppress()
another_kw = pp.Keyword('ANOTHER_KEYWORD:').suppress()
kw = pp.Optional(some_kw ^ another_kw)

# Hint from: http://pyparsing.wikispaces.com/message/view/home/21931601
lines = kw + pp.SkipTo(
    pp.LineEnd() + pp.OneOrMore(pp.LineEnd()) |
    pp.LineEnd() + pp.StringEnd() |
    pp.StringEnd()
)

result = lines.searchString(data.strip())
results_list = result.asList()
# => [['\nline 1\nline 2\nline 3\nline 4'], ['\nline a\nline b\nline c']]

When building a grammar it really helps to assign parts to variables and reference those when you can.



来源:https://stackoverflow.com/questions/5822709/matching-nonempty-lines-with-pyparsing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!