Вот мой пример кода:
Код: Выделить всё
import pyparsing as pp
import textwrap as tw
text = (
"""
A) Red
B) Green
C) Blue
"""
)
a = pp.AtLineStart("A)") + pp.Word(pp.alphas)
b = pp.AtLineStart("B)") + pp.Word(pp.alphas)
c = pp.AtLineStart("C)") + pp.Word(pp.alphas)
grammar = a + b + c
grammar.run_tests(tw.dedent(text).strip())
Код: Выделить всё
A) Red
A) Red
^
ParseException: not found at line start, found end of text (at char 6), (line:1, col:7)
FAIL: not found at line start, found end of text (at char 6), (line:1, col:7)
B) Green
B) Green
^
ParseException: Expected 'A)', found 'B' (at char 0), (line:1, col:1)
FAIL: Expected 'A)', found 'B' (at char 0), (line:1, col:1)
C) Blue
C) Blue
^
ParseException: Expected 'A)', found 'C' (at char 0), (line:1, col:1)
FAIL: Expected 'A)', found 'C' (at char 0), (line:1, col:1)
(Примечание: textwrap.dedent() и Strip() не влияют на результаты этого скрипта.)
Подробнее здесь: https://stackoverflow.com/questions/786 ... -to-basics