第八章 标准库二

第二部分涵盖了很多更能满足专业开发者需求的高级模块。这些模块在小脚本中非常少出现。

8.1 输出格式化

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.

Local模块用来訪问特殊数据格式的文化数据库。Local的分组格式化函数属性为数字的分组分隔格式化提供了直接的方法。

>>> 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’

Python3.2官方文档翻译--输出格式化的更多相关文章

  1. Python3.2官方文档翻译--标准库概览(一)

    7.1 操作系统接口 Os模块提供主要很多与操作系统交互的函数. >>> import os >>> os.getcwd() # Return the curren ...

  2. Python3.2官方文档翻译--继承

    6.5 继承 当然,一门语言特性假设不支持继承那么名称类就失去了价值.子类继承父类的方法例如以下: class DerivedClassName(BaseClassName): <stateme ...

  3. Python3.2官方文档翻译--作用域和命名空间

    6.2 Python作用域和命名空间 在介绍类之前.首先我想告诉你一些关于python作用域的规则. 类的定义很巧妙地运用了命名空间,你须要知道范围和命名空间的工作原理以能全面了解接下来发生的. 顺便 ...

  4. Python3.2官方文档翻译--实例对象和方法对象

    6.3.3 实例对象 如今我们用实例对象做什么呢?实例对象唯一可用的操作就是属性引用.如今有两种合法的属性名称:数据属性和方法. 数据属性相当于smallTalk中的实例变量,C++中的数据成员.数据 ...

  5. Python3.2官方文档翻译--迭代器

    6.9 迭代器 到眼下为止.你可能已注意到很多容器对象都能够用for语句进行循环: 这样的訪问风格清楚简洁方便. 迭代器的应用是python遍历统一.在这样的场景背后.for语句调用容器对象iter( ...

  6. Python3.2官方文档翻译--作用域和命名空间实例

    6.2.1 作用域和命名空间实例 以下的实例主要用来示范怎样引用不同的作用域和命名空间,keywordglobal和nonlocalru怎样影响变量绑定. 实例执行结果是: After local a ...

  7. Spring官方文档翻译(1~6章)

    Spring官方文档翻译(1~6章) 转载至 http://blog.csdn.net/tangtong1/article/details/51326887 Spring官方文档.参考中文文档 一.S ...

  8. Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)(二)

    Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Logging raw data(记录原始数据) Logging the raw ...

  9. Aircrack-ng官方文档翻译[中英对照]---Airmon-ng

    Aircrack-ng官方文档翻译---Airmon-ng Description[简介] This script can be used to enable monitor mode on wire ...

随机推荐

  1. TSearch & TFileSearch Version 2.2 -Boyer-Moore-Horspool search algorithm

    unit Searches; (*-----------------------------------------------------------------------------* | Co ...

  2. java项目怎样添加jar包依赖?

    工程->右键properties->add external jars

  3. Android与Unity交互研究

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/46733221 Android与Unity交互研究 unity与android交互的由来 ...

  4. 利用tca时间聚簇分析方法分析fmri数据

    一.利用ica进行fmri数据分解时,在得到相互独立的成分后,这些成分的后续处理,其实是有很多文章可以做的.比如,对这些成分进行排序和选择.如果能够提出某种方法,能够自动地制造特征,并将这些特征与分解 ...

  5. matlab 图像常用函数

    Canny function [ canny ] = canny( rgb ) temp=rgb2gray(rgb); canny=edge(temp,'canny'); end 灰度 temp=rg ...

  6. nose的setup和teardown

    参考:http://blog.csdn.net/linda1000/article/details/8533349 1.模块的setUp和tearDown def setUp(): print &qu ...

  7. go语言基础之字符串类型 和 字符与字符串类型的区别

    1.字符串类型 示例1: package main //必须有一个main包 import "fmt" func main() { var str1 string str1 = & ...

  8. 零基础学python-3.5 内存管理

    * 变量无需事先声明 * 变量无需指定类型 * 程序猿不用关系内存管理 * 变量名会被回收 * del能够直接释放资源 1.python使用的是引用调用,而不是值调用,他使用的回收算法是引用计数算法, ...

  9. 【crontab】“bad minute”及“errors in crontab file, can't install”错误处理

    今天有朋友提到,在使用crontab定制后台定时备份任务时报出“bad minute”及“errors in crontab file, can't install”错误.经确认,根本原因是cront ...

  10. Python mongoHelper模块

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Defines a MongoOperator class and allows you to m ...