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

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -