Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pyparsing解析命名的嵌套表达式_Python_Pyparsing - Fatal编程技术网

Python 使用pyparsing解析命名的嵌套表达式

Python 使用pyparsing解析命名的嵌套表达式,python,pyparsing,Python,Pyparsing,我尝试使用pyparsing解析一些数据,它看起来(或多或少)像这样: User.Name = Dave User.Age = 42 Date = 2015/01/01 Begin Component List Begin Component 2 1 some data = a value 2 another key = 999 End Component 2 Begin Another Component a.key = 42 End A

我尝试使用pyparsing解析一些数据,它看起来(或多或少)像这样:

User.Name = Dave
User.Age  = 42
Date      = 2015/01/01
Begin Component List
  Begin Component 2
    1 some data   = a value
    2 another key = 999
  End Component 2
  Begin Another Component
    a.key = 42
  End Another Component
End Component List
Begin MoreData
    Another = KeyPair
End MoreData
我发现了一些类似的例子,但我自己做得不是很好

到目前为止,我一直遇到类似的错误:
pyparsing.ParseException:Expected“End”(在char26处),(第5行,第1列)

捕获“Begin”之后的单词很重要,因为它们是字典的名称以及键值对。如果开瓶器后面有一个数字,例如“开始组件2”,则“2”是我不需要的对数(可能是原始软件使用的)。同样,我不需要列表中的数字(“1”和“2”)

nestedExpr
是正确的方法吗

from pyparsing import *

data = '''Begin A
hello
world
End A
'''

opener = Literal('Begin') + Word(alphas)
closer = Literal('End') + Word(alphas)
content = Combine(OneOrMore(~opener
                            + ~closer
                            + CharsNotIn('\n', exact=1)))
expr = nestedExpr(opener=opener, closer=closer, content=content)

parser = expr

res = parser.parseString(data)
print(res)