使用 pyparsing 匹配非空行

发布于 2024-11-04 13:51:50 字数 585 浏览 0 评论 0原文

我正在尝试制作一个小型应用程序,它使用 pyparsing 从另一个程序生成的文件中提取数据。

这些文件具有以下格式。

SOME_KEYWORD:
line 1
line 2
line 3
line 4

ANOTHER_KEYWORD:
line a
line b
line c

我如何构造有助于提取第1行第2行 ... 第4行第a行的语法> .. c 行? 我正在尝试构建这样的结构

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

,但我不知道如何定义 NonEmptyLinesEmptyLine。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骑趴 2024-11-11 13:51:50

我的看法:

    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

结果是:

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

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']]
酷炫老祖宗 2024-11-11 13:51:50

这将带您完成大部分工作:

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']]

构建语法时,将部分分配给变量并在可以时引用这些变量确实很有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文