代码之家  ›  专栏  ›  技术社区  ›  torger

如何使用pyparsing解析和散列由特殊字符包围的字符串?

  •  0
  • torger  · 技术社区  · 16 年前

    我看到的大多数pyparsing示例都涉及线性表达式。

    A=1+2

    我想解析mediawiki标题,并将它们散列到它们的部分。

    例如

    Introduction goes here
    ==Hello==
    foo
    foo
    ===World===
    bar
    bar
    

    dict看起来像:

    {'Introduction':'Whoot introduction goes here', 'Hello':"foo\nfoo", 'World':"bar\nbar"}
    

    如果我能看到这个“封闭式”(==headline==)解析的一个例子,我就可以转到链接/图像/文件等。

    2 回复  |  直到 16 年前
        1
  •  3
  •   Jonathan Feinberg    16 年前

    你错过了吗 this wiki-like language parser 在pyparsing网站示例中?

    h2 = QuotedString("==")
    
        2
  •  1
  •   PaulMcG    16 年前

    此外,此格式与.ini文件不同:

    [section1]
    a = 1
    b = 3
    [section2]
    blah=a
    

    可以使用 this example code .