json - python pretty print dictionary with multiline text in a value? -


here dict \n in values:

d = {'trace': '\n\n##### thread stack trace #####\n  file "/usr/lib/python2.7/threading.py", line 525, in __bootstrap\n    self.__bootstrap_inner()\n  file "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner\n    self.run()\n  file "/usr/lib/python2.7/threading.py", line 505, in run\n    self.__target(*self.__args, **self.__kwargs)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 303, in run_forever\n    self.process()\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/common.py", line 170, in execute\n    return func(*args, **kwargs)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test_plugin/system/schedule.py", line 153, in process\n    self.sleep(delta_ts)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 292, in sleep\n    self.cancelled.wait(interval)\n  file "/usr/lib/python2.7/threading.py", line 404, in wait\n    self.__cond.wait(timeout)\n  file "/usr/lib/python2.7/threading.py", line 263, in wait\n    _sleep(delay)\n'} 

want pretty print it, when try

in [47]: print d {'trace': '\n\n##### thread stack trace #####\n  file "/usr/lib/python2.7/threading.py", line 525, in __bootstrap\n    self.__bootstrap_inner()\n  file "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner\n    self.run()\n  file "/usr/lib/python2.7/threading.py", line 505, in run\n    self.__target(*self.__args, **self.__kwargs)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 303, in run_forever\n    self.process()\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/common.py", line 170, in execute\n    return func(*args, **kwargs)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test_plugin/system/schedule.py", line 153, in process\n    self.sleep(delta_ts)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 292, in sleep\n    self.cancelled.wait(interval)\n  file "/usr/lib/python2.7/threading.py", line 404, in wait\n    self.__cond.wait(timeout)\n  file "/usr/lib/python2.7/threading.py", line 263, in wait\n    _sleep(delay)\n'}  in [48]: import json  in [49]: print json.dumps(d, indent=4) {     "trace": "\n\n##### thread stack trace #####\n  file \"/usr/lib/python2.7/threading.py\", line 525, in __bootstrap\n    self.__bootstrap_inner()\n  file \"/usr/lib/python2.7/threading.py\", line 552, in __bootstrap_inner\n    self.run()\n  file \"/usr/lib/python2.7/threading.py\", line 505, in run\n    self.__target(*self.__args, **self.__kwargs)\n  file \"/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py\", line 303, in run_forever\n    self.process()\n  file \"/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/common.py\", line 170, in execute\n    return func(*args, **kwargs)\n  file \"/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test_plugin/system/schedule.py\", line 153, in process\n    self.sleep(delta_ts)\n  file \"/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py\", line 292, in sleep\n    self.cancelled.wait(interval)\n  file \"/usr/lib/python2.7/threading.py\", line 404, in wait\n    self.__cond.wait(timeout)\n  file \"/usr/lib/python2.7/threading.py\", line 263, in wait\n    _sleep(delay)\n" }   in [50]: pprint import pprint  in [51]: pprint(d) {'trace': '\n\n##### thread stack trace #####\n  file "/usr/lib/python2.7/threading.py", line 525, in __bootstrap\n    self.__bootstrap_inner()\n  file "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner\n    self.run()\n  file "/usr/lib/python2.7/threading.py", line 505, in run\n    self.__target(*self.__args, **self.__kwargs)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 303, in run_forever\n    self.process()\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/common.py", line 170, in execute\n    return func(*args, **kwargs)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test_plugin/system/schedule.py", line 153, in process\n    self.sleep(delta_ts)\n  file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 292, in sleep\n    self.cancelled.wait(interval)\n  file "/usr/lib/python2.7/threading.py", line 404, in wait\n    self.__cond.wait(timeout)\n  file "/usr/lib/python2.7/threading.py", line 263, in wait\n    _sleep(delay)\n'} 

all of above not work, how pretty print this:

{u'trace': '''  ##### thread stack trace #####   file "/usr/lib/python2.7/threading.py", line 525, in __bootstrap     self.__bootstrap_inner()   file "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner     self.run()   file "/usr/lib/python2.7/threading.py", line 505, in run     self.__target(*self.__args, **self.__kwargs)   file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 389, in run_forever     self.sleep(wait_time)   file "/usr/local/test_virtualenv/local/lib/python2.7/site-packages/test/plugin.py", line 292, in sleep     self.cancelled.wait(interval)   file "/usr/lib/python2.7/threading.py", line 404, in wait     self.__cond.wait(timeout)   file "/usr/lib/python2.7/threading.py", line 263, in wait     _sleep(delay)  ''' } 

assuming it's not nested, can do:

def my_pprint(d):     print "{" + ",\n".join("{}: '{}'".format(repr(k), v) k,v in d.items()) + "}" 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -