python漂亮的打印字典与多行文本在一个值?

时间:2016-05-09 09:54:25

标签: python json python-2.7 dictionary

这是一个值为\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)

'''
}

2 个答案:

答案 0 :(得分:0)

假设它没有嵌套,你可以这样做:

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

答案 1 :(得分:0)

一个简单的两行代码可以为您的dict工作,包括值中的\ 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)

其他方法是覆盖dict对象的print方法。

这些答案可能对此有所帮助。

How to "perfectly" override a dict?

Subclassing Python dictionary to override __setitem__