【问题标题】:A parser with Lepl带有 Lepl 的解析器
【发布时间】:2010-11-28 09:52:59
【问题描述】:

我想解析一个文本文件,例如这样的:

div::
    class:yo-d
    text:example
    id:my-class
    h1:: Title
        href:http://www.example.com
    div::
        class:class1
        id:my-class2

类似于reStructuredText。
每个标签都以::结尾并且可以有一些属性attr:value。 我想获得这样的东西,一个 Python 字典:

{'div': {'attrs': {'text': 'example', 'class': 'yo-d', 'id': 'my-class'},
         'sub': {'h1': {'content': 'Title', 'attrs': {'href': 'http://www.example.com'}},
                 'div': {'attrs': {'class': 'class1', 'id': 'my-class2'}},
                },
        }
}

sub之后是缩进的标签,如果标签的::后面有东西,它就会进入'content'

我会使用 Lepl,但我什至不知道从哪里开始,有什么建议吗?

谢谢,
魔方

【问题讨论】:

标签: python parsing lepl


【解决方案1】:

使用 Lepl 的替代方法是 Pyparsing:https://github.com/pyparsing/pyparsing

我目前正在成功使用 Pyparsing,如果你命名结果,你可以得到命名结果的字典。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-10-27
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 2017-04-18
  • 2016-02-24
相关资源
最近更新 更多