更优雅的字符串print——pprint库的使用

pprint是一个python下的输出格式化库,可以美化输出字符串的格式,主要是实现对字符串换行宽度、缩进、嵌套对象打印深度的控制,此外还可以实现对np数组实现更为紧凑的输出。下面对pprint库的使用做一个详细的介绍

1、安装pprint

pip install pprint

2、基本使用

mylist = ["Semantic segmentation is a fundamental task in computer vision and enables many downstream applications.",
          "It is related to image classification since it produces per-pixel category prediction instead of image-level prediction.", 
          "The proposed MLP decoder aggregates information from different layers, ", 
          "and thus combining both local attention and global attention to render powerful representation"]
print(mylist)
pprint.pprint(mylist)
输出信息如下所示:
['Semantic segmentation is a fundamental task in computer vision and enables many downstream applications.', 'It is related to image classification since it produces per-pixel category prediction instead of image-level prediction.', 'The proposed MLP decoder aggregates information from different layers, ', 'and thus combining both local attention and global attention to render powerful representation']
['Semantic segmentation is a fundamental task in computer vision and enables '
 'many downstream applications.',
 'It is related to image classification since it produces per-pixel category '
 'prediction instead of image-level prediction.',
 'The proposed MLP decoder aggregates information from different layers, ',
 'and thus combining both local attention and global attention to render '
 'powerful representation']

3、设置字符缩进

#设置字符缩进,默认值0
pprint.pprint(mylist, indent=4)
输出信息如下所示:
[   'Semantic segmentation is a fundamental task in computer vision and '
    'enables many downstream applications.',
    'It is related to image classification since it produces per-pixel '
    'category prediction instead of image-level prediction.',
    'The proposed MLP decoder aggregates information from different layers, ',
    'and thus combining both local attention and global attention to render '
    'powerful representation']

4、设置字符串换行宽度

#设置字符串换行宽度,默认值80个字符
pprint.pprint(mylist, width=150)
输出信息如下所示:
['Semantic segmentation is a fundamental task in computer vision and enables many downstream applications.',
 'It is related to image classification since it produces per-pixel category prediction instead of image-level prediction.',
 'The proposed MLP decoder aggregates information from different layers, ',
 'and thus combining both local attention and global attention to render powerful representation']

5、设置输出数据的深度

#输出数据的深度控制,用于控制json、dict、list、set、tuple等包含嵌套的对象深度
newlist = [1, [2, [3, [4, [5, [6]]]]]]
print("print:\n",newlist)
print("pprint.pprint:")
pprint.pprint(newlist, depth=3)

dic1={"a1":1,"b1":2,"dic":{"a2":1,"b2":2,"dic":{"a3":1,"b3":2,"dic":{"a4":1,"b4":2}}}}
print("print:\n",dic1)
print("pprint.pprint:")
pprint.pprint(dic1, depth=3,width=40)
输出信息如下所示:
print:
 [1, [2, [3, [4, [5, [6]]]]]]
pprint.pprint:
[1, [2, [3, [...]]]]
print:
 {'a1': 1, 'b1': 2, 'dic': {'a2': 1, 'b2': 2, 'dic': {'a3': 1, 'b3': 2, 'dic': {'a4': 1, 'b4': 2}}}}
pprint.pprint:
{'a1': 1,
 'b1': 2,
 'dic': {'a2': 1,
         'b2': 2,
         'dic': {'a3': 1,
                 'b3': 2,
                 'dic': {...}}}}

6、np数组的美化输出

import numpy as np
ss=np.random.randint(0,5,size=(2,2,2,2,2))
ss=np.random.uniform(-1,1,size=(2,2,2,2,2))
ss=np.around(ss,3)#只保留数据的前三位小数
print("np数组原始输出:\n",ss)
#直接传入np数组是无法有效控制打印数据的格式的,因为print函数会调用numpy对象内置的str方法,把数组转化为特定格式的字符串
print("pprint美化输出1:")
pprint.pprint(ss.tolist())
print("pprint美化输出2: , depth=3")
pprint.pprint(ss.tolist(), depth=3)
输出信息如下所示:
np数组原始输出:
 [[[[[ 0.679 -0.762]
    [ 0.411 -0.892]]

   [[ 0.505  0.286]
    [-0.132 -0.755]]]


  [[[ 0.384  0.995]
    [ 0.254  0.074]]

   [[ 0.562 -0.683]
    [-0.112 -0.027]]]]



 [[[[-0.522 -0.017]
    [ 0.881  0.876]]

   [[ 0.444 -0.963]
    [-0.391  0.438]]]


  [[[ 0.057 -0.593]
    [ 0.853 -0.642]]

   [[-0.933 -0.67 ]
    [-0.671 -0.632]]]]]
pprint美化输出1:
[[[[[0.679, -0.762], [0.411, -0.892]], [[0.505, 0.286], [-0.132, -0.755]]],
  [[[0.384, 0.995], [0.254, 0.074]], [[0.562, -0.683], [-0.112, -0.027]]]],
 [[[[-0.522, -0.017], [0.881, 0.876]], [[0.444, -0.963], [-0.391, 0.438]]],
  [[[0.057, -0.593], [0.853, -0.642]], [[-0.933, -0.67], [-0.671, -0.632]]]]]
pprint美化输出2: , depth=3
[[[[...], [...]], [[...], [...]]], [[[...], [...]], [[...], [...]]]]

使用pprint.pprint()对于np数组无法保证小数点位对齐,使用以下语句可以使小数点位的基本对齐

print(pprint.pformat(ss.tolist()))
输出信息如下所示:
[[[[[-0.119, 0.725], [-0.73, -0.262]], [[-0.221, 0.861], [-0.012, -0.479]]],
  [[[-0.916, 0.529], [-0.885, 0.773]], [[0.219, 0.698], [0.499, -0.95]]]],
 [[[[-0.268, 0.62], [-0.757, -0.81]], [[0.675, 0.008], [-0.911, -0.225]]],
  [[[-0.385, 0.891], [0.29, 0.783]], [[-0.761, -0.411], [0.859, 0.536]]]]]

如果想要更漂亮的对齐效果,可以自己给字符串添加空格填充,具体操作如图。

ss=np.random.uniform(-1,1,size=(2,2,2,2,2))
ss=np.around(ss,2)#只保留数据的前两位小数
strs=pprint.pformat(ss.tolist())
strs=strs.replace('[0','[ 0').replace(' -','-')
print(strs)
输出信息如下所示:
[[[[[-0.56,-0.04], [-0.35,-0.64]], [[ 0.67,-0.45], [ 0.27, 0.8]]],
  [[[ 0.93, 0.21], [ 0.77,-0.66]], [[-0.37,-0.45], [ 0.44,-0.55]]]],
 [[[[ 0.63, 0.09], [ 0.98, 0.11]], [[-0.13, 0.09], [-0.92,-0.92]]],
  [[[ 0.7,-0.81], [-0.54, 0.24]], [[-0.14, 0.93], [ 0.84, 0.0]]]]]

对于非numpy数组的输出精度控制,可以参考python工具方法 9 控制print中输出任意数据的精度,支持numpy数据、tuple、list、dict、set_a486259的博客-CSDN博客_python 打印列表 控制精度

使用上文的方法进行转换后,可以用pprint进行输出

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2022年3月16日 下午4:49
下一篇 2022年3月16日 下午5:06

相关推荐