I have a configuration text that looks like this:
text=""" key1 value1 key2 { value1 value2 } key3 subkey1 { key1 1 key2 2 key3 { value1 } } BLOBKEY name { dont { # comment parse { me } } } key3 subkey2 { key1 value1 } """
Values ββare simple strings or quotation marks. Keys are just alphanumeric strings. I know in advance that key2 and key3.subkey1.key4 will contain sets, so I can interpret these paths differently. Similarly, I know that BLOBKEY will contain a "escaped" section.
The goal is to convert it to a dictionary that looks like this:
{'key1': 'value1', 'key2': set(['value1', 'value2']), 'key3': { 'subkey1': { 'key1': 1, 'key2': 2, 'key3': set(['value1']), }, 'subkey2': { 'key1': 'value1' } }, 'BLOBKEY': { 'name': " dont {\n # comment\n parse { me }\n }\n" } }
This code below does a pretty good job breaking it into a bunch of nested lists.
import pyparsing string = pyparsing.CharsNotIn("{} \t\r\n") group = pyparsing.Forward() group << ( pyparsing.Group(pyparsing.Literal("{").suppress() + pyparsing.ZeroOrMore(group) + pyparsing.Literal("}").suppress()) | string ) toplevel = pyparsing.OneOrMore(group)
What is the best way to get the result I want in Python using pyparsing?
source share