Parsing nested expression names with pirage
I'm trying to parse some data using pyparsing, which looks (more or less) like this:
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
I found a few similar examples, but I have not worked very well.
parsing file with twisted puffs Parse line data up to keyword with pyparsing
Here is what I have so far, but I keep running into an error like: pyparsing.ParseException: Expected "End" (at char 26), (line:5, col:1)
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)
It is important that the words "Begin" are captured, as these are dictionary names and also key-value pairs. If there is a number after opening, for example "Start Component 2" "2" is the number of pairs I don't need (presumably this is used by the original software?). Likewise, I don't need the numbers in the list ("1" and "2").
Is there a nestedExpr
correct approach to this?
source to share
No one has answered this question yet
See similar questions:
or similar: