A parser with Lepl
I want to开发者_JS百科 parse a text file, for example, something like this:
div::
class:yo-d
text:example
id:my-class
h1:: Title
href:http://www.example.com
div::
class:class1
id:my-class2
It is similar to reStructuredText.
Every tag ends with::
and can have some attributes attr:value
.
I want to obtain something like this, a Python dictionary:
{'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'}},
},
}
}
After sub there are the indented tags, and if something follows a tag's ::
it goes in 'content'
.
I would use Lepl, but I don't even know where to start, any suggestions?
Thanks,
rubikAn alternative to using Lepl is Pyparsing: https://github.com/pyparsing/pyparsing
I am currently using Pyparsing successfully, and if you name the results you can get out dictionaries of named results.
精彩评论