I\'m trying to parse strings of the form:
\'foo(bar:baz;x:y)\'
I\'d like the results to be returned in form of a nested dictionary, i.e. fo
Two problems:
pp.Dict
around pp.delimitedList
to make asDict
on the inner result work correctlyasDict
on the outermost ParsingResult
instance, leaving the inner ParsingResult
"uninterpreted"I tried the following:
from pyparsing import *
field_name = field_val = Word(alphanums)
colon = Suppress(Literal(':'))
expr = Dict(Group(
field_name +
nestedExpr(content =
Dict(delimitedList(
Group(field_name + colon + field_value),
delim = ';'
))
)
))
Then used it like this:
>>> res = expr.parseString('foo(bar:baz;x:y)')
>>> type(res['foo'])
<class 'pyparsing.ParseResults'>
>>> { k:v.asDict() for k,v in res.asDict().items() }
{'foo': {'x': 'y', 'bar': 'baz'}}