python18:基础库概述续

这篇博客介绍了Python的一些高级模块,包括输出格式化、模板引擎、处理二进制数据、多线程技术、日志管理、弱引用概念、列表操作工具以及十进制浮点数的算术运算,这些内容适用于专业级编程场景。
摘要由CSDN通过智能技术生成

这里介绍Python的高级模块,用于专家级编程的需要,在小的脚本中很少使用。

输出格式化

reprlib模块提供了repr()的一个版本,定制大的或者多层嵌套的容器的简洁展示:
>>> import reprlib
>>> reprlib.repr(set('supercalifragilisticexpialidocious'))
"set(['a', 'c', 'd', 'e', 'f', 'g', ...])"
pprint模块为打印内建的和用户自定义的对象上提供了更复杂的控制,当结果超过了一行,“美化打印”增加了分行机制以更清晰的展示数据结构:
>>> import pprint
>>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',
        'yellow'], 'blue']]]
   
>>> pprint.pprint(t, width=30)
[[[['black', 'cyan'],
   'white',
   ['green', 'red']],
  [['magenta', 'yellow'],
   'blue']]]
textwrap模块格式化文章的段落以适应屏幕宽度:
>>> import textwrap
>>> doc = """The wrap() method is just like fill() except that it returns
    a list of strings instead of one big string with newlines to separate
    the wrapped lines."""
   
>>> print(textwrap.fill(doc, width=40))
The wrap() method is just like fill()
except that it returns a list of strings
instead of one big string with newlines
to separate the wrapped lines.
locale模块进入一个文化特定数据格式的数据库,locale的格式化函数的grouping属性提供了一个使用组分隔符格式化数字的方法:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv()          # get a mapping of conventions
>>> x = 1234567.8
>>> locale.format("%d", x, grouping=True)
'1,234,567'
>>> locale.format_string("%s%.*f", (conv['currency_symbol'],
                         conv['frac_digits'], x), grouping=True)
'$1,234,567.80'

模板

String模块包含了一个通用的Template类,简化语法适用于被终端用户编辑。这允许用户不用改变应用来定制他们的应用。
该类使用占位符$和有效的Python标识符(字母、数字和下划线)。带括号的占位符允许被多个无空格的字母数字串跟随。使用$$
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值