简体   繁体   中英

Nested dictionaries in Pyparsing Parseresults

I am using Pyparsing expression of the following type.

pp.SkipTo(common_cfg)('value 2') + common_cfg + pp.SkipTo(pp.LineEnd())

common_cfg is an expression that assigns 'value 1' to its result.

When I parse it asDict() I am getting nested Dictionaries. Why is it happening and how can I get around it? I just need an empty string in case of "value 2".

{'value 1': '52D4B6ED', 'value 2': ([''], {})}

The value you are seeing for "value 2" is not a nested dictionary. You are getting a ParseResults object containing a single entry, an empty string. Since ParseResults have characteristics of both lists and dicts, its repr string shows both, first the list of parsed strings or objects, followed by a dict-like listing of any named results and their values. Print out type(result["value 2"]) to see this, or result["value 2"].dump() .

You may have some success by "ungrouping" the SkipTo results, using:

pp.ungroup(pp.SkipTo(common_cfg)('value 2'))

for the first term in your parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM