2016-05-09 86 views
0

這裏是價值觀與\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) 

''' 
} 
+0

你漂亮地打印預期輸出是無效的Python或JSON的語法,這是其他格式生產什麼。您可以將輸出顯示爲三重引號的字符串,但我不知道任何*已經可用的*函數或庫可以產生此結果。 –

回答

0

假設它沒有嵌套,你可以這樣做:

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

一個簡單的兩行代碼可以爲你的代碼做你的工作,包括\ n在你的代碼中。

def print_dict(d): 
    str= '{' 
    for k,v in d.items(): 
     str+="\n\'{}\' : {}".format(k,v) 
    str+= '}' 
    print str 

另一種方式來打印漂亮使用Python內置庫pprint。

import pprint 
print pprint.pformat(dict_object) 

其他方法是重寫字典對象的打印方法。

這些答案可能會幫助你。

How to "perfectly" override a dict?

Subclassing Python dictionary to override __setitem__