fork download
  1. import pprint
  2. import re
  3. import sys
  4.  
  5. data = sys.stdin.read()
  6.  
  7. exec (data.replace('(', 'dict(')) # use only for input that you control e.g., for unit-testing
  8. pprint.pprint(abc)
  9.  
  10. def repl_flat(m):
  11. return "\n".join("<{0}>{1}</{0}>".format(*map(str.strip, s.partition('=')[::2]))
  12. for s in m.group(1).split(','))
  13.  
  14. def eval_nested(expr):
  15. val, n = re.subn(r"\(([^)(]+)\)", repl_flat, expr)
  16. return val if n == 0 else eval_nested(val)
  17.  
  18. print eval_nested("(%s)" % (data,))
Success #stdin #stdout 0.02s 4780KB
stdin
abc = (  
        bcd = (efg = 0, ghr = 5, lmn = 10), 
        ghd = 5, 
        zde = (dfs = 10, fge =20, dfg = (sdf = 3, ert = 5), juh = 0))
stdout
{'bcd': {'efg': 0, 'ghr': 5, 'lmn': 10},
 'ghd': 5,
 'zde': {'dfg': {'ert': 5, 'sdf': 3}, 'dfs': 10, 'fge': 20, 'juh': 0}}
<abc><bcd><efg>0</efg>
<ghr>5</ghr>
<lmn>10</lmn></bcd>
<ghd>5</ghd>
<zde><dfs>10</dfs>
<fge>20</fge>
<dfg><sdf>3</sdf>
<ert>5</ert></dfg>
<juh>0</juh></zde></abc>