Python3.2官方文档翻译--输出格式化
第八章 标准库二
第二部分涵盖了很多更能满足专业开发者需求的高级模块。这些模块在小脚本中非常少出现。
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官方文档翻译--输出格式化的更多相关文章
- Python3.2官方文档翻译--标准库概览(一)
7.1 操作系统接口 Os模块提供主要很多与操作系统交互的函数. >>> import os >>> os.getcwd() # Return the curren ...
- Python3.2官方文档翻译--继承
6.5 继承 当然,一门语言特性假设不支持继承那么名称类就失去了价值.子类继承父类的方法例如以下: class DerivedClassName(BaseClassName): <stateme ...
- Python3.2官方文档翻译--作用域和命名空间
6.2 Python作用域和命名空间 在介绍类之前.首先我想告诉你一些关于python作用域的规则. 类的定义很巧妙地运用了命名空间,你须要知道范围和命名空间的工作原理以能全面了解接下来发生的. 顺便 ...
- Python3.2官方文档翻译--实例对象和方法对象
6.3.3 实例对象 如今我们用实例对象做什么呢?实例对象唯一可用的操作就是属性引用.如今有两种合法的属性名称:数据属性和方法. 数据属性相当于smallTalk中的实例变量,C++中的数据成员.数据 ...
- Python3.2官方文档翻译--迭代器
6.9 迭代器 到眼下为止.你可能已注意到很多容器对象都能够用for语句进行循环: 这样的訪问风格清楚简洁方便. 迭代器的应用是python遍历统一.在这样的场景背后.for语句调用容器对象iter( ...
- Python3.2官方文档翻译--作用域和命名空间实例
6.2.1 作用域和命名空间实例 以下的实例主要用来示范怎样引用不同的作用域和命名空间,keywordglobal和nonlocalru怎样影响变量绑定. 实例执行结果是: After local a ...
- Spring官方文档翻译(1~6章)
Spring官方文档翻译(1~6章) 转载至 http://blog.csdn.net/tangtong1/article/details/51326887 Spring官方文档.参考中文文档 一.S ...
- Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)(二)
Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Logging raw data(记录原始数据) Logging the raw ...
- Aircrack-ng官方文档翻译[中英对照]---Airmon-ng
Aircrack-ng官方文档翻译---Airmon-ng Description[简介] This script can be used to enable monitor mode on wire ...
随机推荐
- 借助JRebel使Tomcat支持热部署
JRebel是一个J2EE热部署的工具.使用它可以减少浪费8-18%的开发时间在项目的构建和部署上.虽然Java也提供了HotSpot的JVM,但是如果你修改的类中有方法名称变动的话,HotSpot就 ...
- Ubuntu下gcc多版本共存和版本切换
https://my.oschina.net/u/2306127/blog/538139 摘要: Ubuntu系统使用的gcc版本随着发布版本的不同而不同,在编译android系统时不同的版本推荐用不 ...
- GCC降级
前阵子将Ubuntu升级到了12.04,原来装得virtualbox也可以正常使用.后来几次内核升级之后,virtualbox突然不能用了.virtualbox提示进行/etc/init.d/vbox ...
- MYSQL 名人博客
: DavidYang的博客 - CSDN.NET DimitriK's (dim) Weblog Xaprb · Stay Curious! 飞鸿无痕的博客 - ChinaUnix博客 何登成的技术 ...
- Unable to load DLL 'opencv_core290'
问题: In my winforms application I need to use some Emgu.CV libraries (I have installed Emgu 2.9). Pro ...
- printf回到上一行开头以及回到本行开头的方法
回到上一行开头 #include <stdio.h> #include <unistd.h> int main(void) { ; ){ printf("%d\n&q ...
- matlab colormap
This table lists the built-in colormaps functions. Colormap Name Color Scale parula
- Android屏幕尺寸适配注意事项
1 基本设置 1.1 AndroidManifest.xml设置 在中添加子元素 android:anyDensity="true"时,应用程序安装在不同密度的终端上时,程序会分别 ...
- 电子书mobi的格式详解
https://wiki.mobileread.com/wiki/MOBI#Format Like PalmDOC, the Mobipocket file format is that of a s ...
- Pydoc 本地 HTML 形式查看
Pydoc 本地 HTML 形式查看 我们在编写Python代码时,常常会去查询某些模块及函数的使用,会选择 dir() 及 help() 函数.或查看 CHM 格式的Python帮助文档.或查看Py ...