Python基础:格式化输出

上一篇 / 下一篇  2024-01-09 11:12:54

  在平时使用python编写代码时,控制台打印结果变量是必不可少的,而且很多初学者都会很频繁的使用python自带的print()方法在代码中打桩,以判断自己的代码逻辑获取值是正确的。
  但是在代码中如果需要打印的字段中的数值非常多的时候,使用print()打印的话就会显得很凌乱,比如下面这种场景。
  data = [{"first": "第一个值", "second": "第二个值", "third": "第三个值", "fourth": "第四个值", "fifth": "第五个值", },
          {"first": [1, 2, 2, 3, 66, 765], "second": {"first":"1","second":"2","third":"3",}, "third": {"first":"1","second":"2","third":"3",}, "fourth": [1,2,3,4,5,], "fifth": ["frist",{"second":{"1":2,"2":"3"}}]}]
  定义一个data变量赋值了一个典型的多维列表,然后每个字典中有多个键值对,这个时候使用print()方法打印:
  >>> print(data)
  #输出结果为
  >>> [{'first': '第一个值', 'second': '第二个值', 'third': '第三个值', 'fourth': '第四个值', 'fifth': '第五个值'}, {'first': [1, 2, 2, 3, 66, 765], 'second': {'first': '1', 'second': '2', 'third': '3'}, 'third': {'first': '1', 'second': '2', 'third': '3'}, 'fourth': [1, 2, 3, 4, 5], 'fifth': ['frist', {'second': {'1': 2, '2': '3'}}]}]
  使用print()打出来的结果直接会显示成为一条并且看起来会很凌乱,而且在pycharm工具中会显示成很长的一条数据,如果这个字典中的多维层级在多一些,我们想要取某一个关键值的就会很麻烦。
  接下来给大家介绍两个其他的打印方法,分别为pprint和ic。
  一、pprint
  pprint(pretty-print)是 Python 标准库中的一个模块,可以将 Python 对象以一种可读性更高的格式打印出来。比起print()函数可以更好的处理复杂的数据结构,例如嵌套的列表、字典等。这里还是以上方的data变量为例,首先使用from pprint import pprint导入pprint,然后使用pprint()方法打印出来的结果如下。
  >>> pprint(data)
  #输出结果为
  >>> [{'fifth': '第五个值',
        'first': '第一个值',
        'fourth': '第四个值',
        'second': '第二个值',
        'third': '第三个值'},
       {'fifth': ['frist', {'second': {'1': 2, '2': '3'}}],
        'first': [1, 2, 2, 3, 66, 765],
        'fourth': [1, 2, 3, 4, 5],
        'second': {'first': '1', 'second': '2', 'third': '3'},
        'third': {'first': '1', 'second': '2', 'third': '3'}}]
  可以清晰的看见,pprint()将打印的结果做了一个格式化的操作,将列表中每个字典键值进行分行显示,使字典的结构显示更加清晰,提高了可阅读性。
  同时pprint()还有一些高级用法,我们可以定义打印数据的缩进、设置输出宽度等。
  1.设置缩进
  pprint()方法中有一个设置缩进的indent参数,如果想要每个层级之间缩进2个空格,则设置indent=2即可,示例如下
  >>> pprint(data,indent=2)
  #输出结果为
  >>> [ { 'fifth': '第五个值',        
         'first': '第一个值',
         'fourth': '第四个值',
         'second': '第二个值',
         'third': '第三个值'},
       { 'fifth': ['frist', {'second': {'1': 2, '2': '3'}}],
        'first': [1, 2, 2, 3, 66, 765],
        'fourth': [1, 2, 3, 4, 5],
        'second': {'first': '1', 'second': '2', 'third': '3'},
        'third': {'first': '1', 'second': '2', 'third': '3'}}]
  可以看见设置indent=2之后,pprint()将打印出的数据每个层级之间都缩进2个空格,这样在平时打印工作中合理设置一个缩进空格可以更加直观的阅读每个层级。
  2.设置宽度
  pprint ()默认的输出宽度为 80 个字符,可以通过设置 width 参数来更改输出宽度。示例如下:
  >>> pprint(data,width=40)
  #输出结果为
  >>> [{'fifth': '第五个值',
        'first': '第一个值',
        'fourth': '第四个值',
        'second': '第二个值',
        'third': '第三个值'},
       {'fifth': ['frist',
                  {'second': {'1': 2,
                              '2': '3'}}],
        'first': [1, 2, 2, 3, 66, 765],
        'fourth': [1, 2, 3, 4, 5],
        'second': {'first': '1',
                   'second': '2',
                   'third': '3'},
        'third': {'first': '1',
                  'second': '2',
                  'third': '3'}}]
  设置输出宽度后可以看到pprint()将打印的数据中每一个字典的键值都分行显示了。
  二、ic
  ic模块取之于icecream库,它是一个python第三方库,在使用去需要下载icecream库并导入:
  #pip安装icecream库
  pip install icecream
  #导入ic模块
  from icecream import ic
  使用ic()方法打印对象数据时,它也是可以将数据进行格式化后打印出来最终的结果,数据格式化这一点和pprint()基本是相同的:
  >>> ic(data)
  #输出结果为
  >>> ic| data: [{'fifth': '第五个值',
                  'first': '第一个值',
                  'fourth': '第四个值',
                  'second': '第二个值',
                  'third': '第三个值'},
                 {'fifth': ['frist', {'second': {'1': 2, '2': '3'}}],
                  'first': [1, 2, 2, 3, 66, 765],
                  'fourth': [1, 2, 3, 4, 5],
                  'second': {'first': '1', 'second': '2', 'third': '3'},
                  'third': {'first': '1', 'second': '2', 'third': '3'}}]
  通过使用ic()方法输出后,不仅可以看到格式话的数据,并且它还自动将你输出的变量也显示了出来,这样也可以延伸到输出函数返回的值,具体如下:
  #随意定义一个函数
  def func(one_data):
      return one_data + 1
      #给函数传入一个值为3,并打印函数返回值
  >>> ic(func(3))
  #得到打印的结果
  >>> ic| func(3): 4
  这样把输出的函数和我们传入的值也自动显示出来了。
  同时,以pycharm为例,使用ic()输出值后,在控制台会自动将你输出的值转变颜色,可以更加直观的看到输出的内容。

TAG: 软件开发 Python

 

评分:0

我来说两句

Open Toolbar