這裏是價值觀與\n
的字典:蟒蛇漂亮打印字典與多行文本中的值?
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 [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]: from 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'}
上述所有的不工作,如何漂亮打印它像這樣:
{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)
'''
}
你漂亮地打印預期輸出是無效的Python或JSON的語法,這是其他格式生產什麼。您可以將輸出顯示爲三重引號的字符串,但我不知道任何*已經可用的*函數或庫可以產生此結果。 –