Here is a somewhat alternative approach that uses the Python function ast.literal_eval :
import ast, re
orig_text = """key1=[subKey1=[val1,val2=[k1,k2]],val3,val4,subKey2=[aaa,bbb]],key2=val5,key3,key4=[1,2,3]"""
quoted_values = re.sub(r'([a-zA-Z0-9]+)', r'"\1"', orig_text)
assignments_removed = re.sub(r'("[a-zA-Z0-9]+?"\s?=\s*)', '', quoted_values)
print ast.literal_eval(assignments_removed)
This will at least give you all the values as follows:
([['val1', ['k1', 'k2']], 'val3', 'val4', ['aaa', 'bbb']], 'val5', 'key3', ['1', '2', '3'])
This works by first quoting all values and then deleting all assignments to allow literal_evalwork. The structure is preserved.
source
share