splitting a braces grouped string in python -
appreciate in one-liner idiom following efficiently.
i have string groups separated braces below:
{1:xxxx}{2:xxxx}{3:{10:xxxx}}{4:xxxx\r\n:xxxx}.... how convert dictionary format?
dict={1:'xxx',2:'xxxx',3:'{10:xxxx}'},4:'xxxx\r\n:xxxx'}
this how it:
raw = """{1:xxxx}{2:xxxx}{3:{10:xxxx}}{4:'xxxx\r\n:xxxx'}""" def parse(raw): # split chunks '}{' , remove outer '{}' parts = raw[1:-1].split('}{') part in parts: # split first ':' num, data = part.split(':', 1) # yield each entry found yield int(num), data # make dict print dict(parse(raw)) it keeps '{10:xxxx}' string in example.
Comments
Post a Comment