如何在 Python 中使用 pprint 缩进?
How to indent with pprint in Python?
我正在尝试缩进 pprint 的输出,以便在 pprint 中得到 8 space 的缩进。我使用的代码是:
import numpy as np
from pprint import pprint
A = np.array([1, 2, 3, 4])
f = open("log.txt", 'w')
n = 2
for i in range(n):
A = A + 1
f.writelines(list(u' \u27B3 - %s\n'.encode('utf-8') % i for i in A))
pprint(globals())
输出
{'A': array([2, 3, 4, 5]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 0,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
{'A': array([3, 4, 5, 6]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 1,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
期望输出
{'A': array([2, 3, 4, 5]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 0,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
{'A': array([3, 4, 5, 6]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 1,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
简而言之,在写入文件或使用 pprint
打印时,我需要 space 缩进。我试过了
pp = pprint.PrettyPrinter(indent=8)
但是没用
您无法pprint()
添加额外的前导缩进,不。
您最好的选择是改用 pprint.pformat()
function,然后手动添加缩进:
from pprint import pformat
print ''.join([
' ' + l
for l in pformat(globals()).splitlines(True)])
这使用 str.splitlines()
method 将 pformat()
输出分成单独的行以便于重新加入。
在Python3中,缩进可以用textwrap.indent()
:
from pprint import pformat
from textwrap import indent
print(indent(pformat(globals()),
' '))
对 Martijn 的回答进行了小幅更新。无需在简单的 str concat 中使用 .format()。
from pprint import pformat
def left_indent(text, indent=' ' * 8):
return ''.join([indent + l for l in text.splitlines(True)])
print(indent(pformat(data)))
我正在尝试缩进 pprint 的输出,以便在 pprint 中得到 8 space 的缩进。我使用的代码是:
import numpy as np
from pprint import pprint
A = np.array([1, 2, 3, 4])
f = open("log.txt", 'w')
n = 2
for i in range(n):
A = A + 1
f.writelines(list(u' \u27B3 - %s\n'.encode('utf-8') % i for i in A))
pprint(globals())
输出
{'A': array([2, 3, 4, 5]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 0,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
{'A': array([3, 4, 5, 6]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 1,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
期望输出
{'A': array([2, 3, 4, 5]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 0,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
{'A': array([3, 4, 5, 6]),
'__builtins__': <module '__builtin__' (built-in)>,
'__doc__': None,
'__file__': '~/Stack exchange/pprint_tab.py',
'__name__': '__main__',
'__package__': None,
'f': <open file 'log.txt', mode 'w' at 0xb659b8b8>,
'i': 1,
'n': 2,
'np': <module 'numpy' from '/usr/lib/python2.7/dist...,
'pprint': <function pprint at 0xb6dea48c>}
简而言之,在写入文件或使用 pprint
打印时,我需要 space 缩进。我试过了
pp = pprint.PrettyPrinter(indent=8)
但是没用
您无法pprint()
添加额外的前导缩进,不。
您最好的选择是改用 pprint.pformat()
function,然后手动添加缩进:
from pprint import pformat
print ''.join([
' ' + l
for l in pformat(globals()).splitlines(True)])
这使用 str.splitlines()
method 将 pformat()
输出分成单独的行以便于重新加入。
在Python3中,缩进可以用textwrap.indent()
:
from pprint import pformat
from textwrap import indent
print(indent(pformat(globals()),
' '))
对 Martijn 的回答进行了小幅更新。无需在简单的 str concat 中使用 .format()。
from pprint import pformat
def left_indent(text, indent=' ' * 8):
return ''.join([indent + l for l in text.splitlines(True)])
print(indent(pformat(data)))