一. pprint美观打印数据结构pprint模块包含一个“美观打印机”,用于生成数据结构的一个美观的视图 。格式化工具会生成数据结构的一些表示,不仅能够由解释器正确地解析,还便于人阅读 。输出会尽可能放在一行上,分解为多行时会缩进 。
1.打印from pprint import pprintdata = https://tazarkount.com/read/[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),(2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H','i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),(3, ['m', 'n']),(4, ['o', 'p', 'q']),(5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),]print('PRINT:')print(data)print()print('PPRINT:')pprint(data)pprint()格式化一个对象,并把它作为参数传入一个数据流(或者是默认的sys.stdout) 。
PRINT:[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]PPRINT:[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2,{'e': 'E','f': 'F','g': 'G','h': 'H','i': 'I','j': 'J','k': 'K','l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]2 .格式化要格式化一个数据结构而不是把它直接写入一个流(即用于日志),可以使用pformat()来构建一个字符串表示 。
import loggingfrom pprint import pformatdata = https://tazarkount.com/read/[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),(2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H','i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),(3, ['m', 'n']),(4, ['o', 'p', 'q']),(5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),]logging.basicConfig(level=logging.DEBUG,format='%(levelname)-8s %(message)s',)logging.debug('Logging pformatted data')formatted = pformat(data)for line in formatted.splitlines():logging.debug(line.rstrip())然后可以单独打印这个格式化的字符串或者记入日志 。
DEBUGLogging pformatted dataDEBUG[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),DEBUG(2,DEBUG{'e': 'E',DEBUG'f': 'F',DEBUG'g': 'G',DEBUG'h': 'H',DEBUG'i': 'I',DEBUG'j': 'J',DEBUG'k': 'K',DEBUG'l': 'L'}),DEBUG(3, ['m', 'n']),DEBUG(4, ['o', 'p', 'q']),DEBUG(5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]3. 任意类如果一个定制类定义了一个__repr__()方法,那么pprint()使用的PrettyPrinter类还可以处理这样的定制类 。
from pprint import pprintclass node:def __init__(self, name, contents=[]):self.name = nameself.contents = contents[:]def __repr__(self):return ('node(' + repr(self.name) + ', ' +repr(self.contents) + ')')trees = [node('node-1'),node('node-2', [node('node-2-1')]),node('node-3', [node('node-3-1')]),]pprint(trees)利用由PrettyPrinter组合的嵌套对象的表示来返回完整的字符串表示 。
[node('node-1', []), node('node-2', [node('node-2-1', [])]), node('node-3', [node('node-3-1', [])])]4. 递归递归数据结构由指向原数据源的引用表示,形式为<Recursion on typename with id=number>
'''学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!'''from pprint import pprintlocal_data = https://tazarkount.com/read/['a', 'b', 1, 2]local_data.append(local_data)print('id(local_data) =>', id(local_data))pprint(local_data)在这个例子中,列表local_data增加到其自身,这会创建一个递归引用 。
id(local_data) => 2763816527488['a', 'b', 1, 2, <Recursion on list with id=2763816527488>]5. 限制嵌套输出对于非常深的数据结构,可能不要求输出中包含所有细节 。数据有可能没有适当地格式化,也可能格式化文本过大而无法管理,或者有些数据可能是多余的 。
from pprint import pprintdata = https://tazarkount.com/read/[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),(2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H','i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),(3, ['m', 'n']),(4, ['o', 'p', 'q']),(5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),]pprint(data, depth=1)pprint(data, depth=2)使用depth参数可以控制美观打印机递归处理嵌套数据结构的深度 。输出中未包含的层次用省略号表示 。
[(...), (...), (...), (...), (...)][(1, {...}), (2, {...}), (3, [...]), (4, [...]), (5, [...])]6.控制输出宽度格式化文本的默认输出宽度为80列 。要调整这个宽度,可以在pprint()中使用参数width 。
from pprint import pprintdata = https://tazarkount.com/read/[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),(2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H','i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),(3, ['m', 'n']),(4, ['o', 'p', 'q']),(5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),]for width in [80, 5]:print('WIDTH =', width)pprint(data, width=width)print()当宽度太小而不能满足格式化数据结构时,倘若截断或转行会导致非法语法,那么便不会截断或转行 。
WIDTH = 80[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2,{'e': 'E','f': 'F','g': 'G','h': 'H','i': 'I','j': 'J','k': 'K','l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]WIDTH = 5[(1,{'a': 'A','b': 'B','c': 'C','d': 'D'}), (2,{'e': 'E','f': 'F','g': 'G','h': 'H','i': 'I','j': 'J','k': 'K','l': 'L'}), (3,['m','n']), (4,['o','p','q']), (5,['r','s','tu','v','x','y','z'])]compact标志告诉pprint()尝试在每一行上放置更多数据,而不是把复杂数据结构分解为多行 。
【介绍一个非常好用的Python模块-pprint模块,相信你一定会爱上它的】from pprint import pprintdata = https://tazarkount.com/read/[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),(2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H','i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),(3, ['m', 'n']),(4, ['o', 'p', 'q']),(5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),]for width in [80, 5]:print('WIDTH =', width)pprint(data, width=width)print()这个例子展示了一个数据结构再一行上放不下时,它会分解(数据列表中的第二项也是如此) 。如果多个元素可以放置在一行上(如第三个和第四个成员),那么便会把它们放在同一行上 。
WIDTH = 80[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2,{'e': 'E','f': 'F','g': 'G','h': 'H','i': 'I','j': 'J','k': 'K','l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]WIDTH = 5[(1,{'a': 'A','b': 'B','c': 'C','d': 'D'}), (2,{'e': 'E','f': 'F','g': 'G','h': 'H','i': 'I','j': 'J','k': 'K','l': 'L'}), (3,['m','n']), (4,['o','p','q']), (5,['r','s','tu','v','x','y','z'])]结尾给大家推荐一个非常好的学习教程,希望对你学习Python有帮助!
Python基础入门教程推荐
Python爬虫案例教程推荐
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
