Парсинг скобок в Python


Первый вариант:

>>> from pyparsing import nestedExpr

>>> txt = "{ { a } { b } { { { c } } } }"

>>>

>>> nestedExpr('{','}').parseString(txt).asList()

[[['a'], ['b'], [[['c']]]]]

>>>

Второй вариант:

Можно еще парсить с помощью lepl (устанавливаемые через $ easy_install lepl):

from lepl import Any, Delayed, Node, Space

expr = Delayed()

expr += '{' / (Any() | expr[1:,Space()[:]]) / '}' > Node

print expr.parse("{{a}{b}{{{c}}}}")[0]
Парсинг скобок в Python Первый вариант: >>> from pyparsing import nestedExpr >>> txt = "{ { a } { b } { { { c } } } }" >>> >>> nestedExpr('{','}').parseString(txt).
126 просмотров