Best way to parse this sequence?

TheInovat :

I get a string like this

EXAMPLE{TEST;ANOTHER{PART1;PART2};UNLIMITED{POSSIBILITIES{LIKE;THIS}}}

and have to get a result like this

EXAMPLETEST
EXAMPLEANOTHERPART1
EXAMPLEANOTHERPART2
EXAMPLEUNLIMITEDPOSSIBILITIESLIKE
EXAMPLEUNLIMITEDPOSSIBILITIESTHIS

Because there could be a unlimited amount of nesting, I'm having troubles remembering the parts before. Could you maybe point me in the right direction ?

Thanks a lot

Tin Nguyen :

Goal: Turn it into a dict. And then create your output from the dictionary.

>>> string = "EXAMPLE{TEST;ANOTHER{PART1;PART2};UNLIMITED{POSSIBILITIES{LIKE;THIS}}}"
>>> string = string.replace(";", ",").replace("{", ": {")
>>> string
'EXAMPLE: {TEST,ANOTHER: {PART1,PART2},UNLIMITED: {POSSIBILITIES: {LIKE,THIS}}}'

EXAMPLE, TEST, ANOTHER are strings but they aren't wrapped in quotation marks "" or ''.

Using RegEx to solve this problem:

>>> import re
>>> string = re.sub(r"(\w+)", r"'\1'", string)
>>> string
"'EXAMPLE': {'TEST','ANOTHER': {'PART1','PART2'},'UNLIMITED': {'POSSIBILITIES': {'LIKE','THIS'}}}"

This is still not a valid file format. It's not JSON. It's not a dict. It's a mixture of a dict and a set in Python.
Making it look more like a dictionary:

>>> string = re.sub(r"'(\w+)',", r"'\1': None, ", string)
>>> string = re.sub(r"'(\w+)'}", r"'\1': None}", string)
>>> string
"'EXAMPLE': {'TEST': None, 'ANOTHER': {'PART1': None, 'PART2': None},'UNLIMITED': {'POSSIBILITIES': {'LIKE': None, 'THIS': None}}}"

Now turning it into a Python object:

>>> my_dict = eval('{' + string + '}')
>>> my_dict
{'EXAMPLE': {'TEST': None, 'ANOTHER': {'PART1': None, 'PART2': None}, 'UNLIMITED': {'POSSIBILITIES': {'LIKE': None, 'THIS': None}}}}

Now you have a regular Python object which you can iterate through and do string manipulation. You can write a recursive function that concatenates your string:

>>> def create_output(dict_element, result):
...     if dict_element == None:
...         print(result)
...         return
...     for key, value in dict_element.items():
...         create_output(value, result + key)
...
>>> create_output(my_dict, "")
EXAMPLETEST
EXAMPLEANOTHERPART1
EXAMPLEANOTHERPART2
EXAMPLEUNLIMITEDPOSSIBILITIESLIKE
EXAMPLEUNLIMITEDPOSSIBILITIESTHIS

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=297290&siteId=1