Как мне получить содержимое ?
Код: Выделить всё
from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor
grammar = Grammar(
r"""
text = (sections)*
sections = ws* '' ws* section_element+ ws* '' ws*
section_element = "" section_content ""
section_content = ws* value ws*
ws = ~"\s*"
value = ~"[ a-z < > \n\r\s].*"
"""
)
class TextVisitor(NodeVisitor):
def visit_text(self, node, visited_children):
return node.text
def visit_section_content(self, node, visited_children):
return node.text
def generic_visit(self, node, visited_children):
return visited_children or node
text = """
aaa bbb
"""
tree = grammar.parse(text)
tv = TextVisitor()
extracted_text = tv.visit(tree)
print(extracted_text)
Код: Выделить всё
text = """
aaa bbb
"""
Код: Выделить всё
text = """
aaa
bbb
"""
Подробнее здесь: https://stackoverflow.com/questions/792 ... rsimonious