python2.7中模块学习- textwrap 文本包装和填充
2016-07-08 17:02
363 查看
1.TextWrap提供函数wrap()和fill(),以及TextWrapper类,工具函数dedent().通常包装或者填充一两个字符串使用wrap()和fill()。其他情况使用TextWrapper更高效。
2.textwrap.wrap(text[,width[, …]]):包装单个段落(text为输入,系字符串),每行最长宽度width。返回输出行的列表,最后行无换行符。Width默认70。
3.textwrap.fill(text[,width[, …]]):包装单段文字,并返回包含包裹段落的字符串。实际上是”\n”.join(wrap(text,…))的缩写。wrap() andfill()创建TextWrapper实例,并调用一个方法。这些实例不被重用,所以包装/填充很多文本字符串要构造自己的TextWrapper对象更有TextWrapper.break_long_words设置是否拆长单词。
4.textwrap.dedent(text):反缩进去除每行行首的空白。这方便显示三引号中的内容而不修改其源代码中的缩进。
代码示例:(注意:在python2.7中没有from textwrap_example import sample_text 的用法,所以我们事先设定一个简单的text字符串)
text=”’ Object for wrapping/filling text. The public interface consists
of the wrap() and fill() methods; the other methods are just there for
subclasses to override in order to tweak the default behaviour.”’
(1)wrap的用法:
从结果看wrap把text分成等长的序列了。
(2)fill的用法:
输出结果为:
Nodedent:
Object for wrapping/filling text.The public
interfaceconsists of the wrap() and fill()
methods; the other methods arejust there for
subclasses to override in order to tweak the
default behaviour.
结果为左对齐,第一行有缩进。行中的空格继续保留。
(3)denent的用法:
输出结果为:
Dedented:
Object for wrapping/filling text.The public
interfaceconsists of the wrap() and fill() methods; the
other methods arejust there for subclasses to override in
order to tweak the default behaviour.
此时已经消除了首行的缩进了。
2.textwrap.wrap(text[,width[, …]]):包装单个段落(text为输入,系字符串),每行最长宽度width。返回输出行的列表,最后行无换行符。Width默认70。
3.textwrap.fill(text[,width[, …]]):包装单段文字,并返回包含包裹段落的字符串。实际上是”\n”.join(wrap(text,…))的缩写。wrap() andfill()创建TextWrapper实例,并调用一个方法。这些实例不被重用,所以包装/填充很多文本字符串要构造自己的TextWrapper对象更有TextWrapper.break_long_words设置是否拆长单词。
4.textwrap.dedent(text):反缩进去除每行行首的空白。这方便显示三引号中的内容而不修改其源代码中的缩进。
代码示例:(注意:在python2.7中没有from textwrap_example import sample_text 的用法,所以我们事先设定一个简单的text字符串)
text=”’ Object for wrapping/filling text. The public interface consists
of the wrap() and fill() methods; the other methods are just there for
subclasses to override in order to tweak the default behaviour.”’
(1)wrap的用法:
>>> import textwrap >>> print textwrap.wrap(text,width=70) [' Object for wrapping/filling text. The public interface consists of', 'the wrap() and fill() methods; the other methods are just there for', 'subclasses to override in order to tweak the default behaviour.'] >>>
从结果看wrap把text分成等长的序列了。
(2)fill的用法:
text=''' Object for wrapping/filling text.The public interfaceconsists of the wrap() and fill() methods; the other methods arejust there for subclasses to override in order to tweak the default behaviour.''' import textwrap print 'Nodedent:\n' print textwrap.fill(text,width=50)
输出结果为:
Nodedent:
Object for wrapping/filling text.The public
interfaceconsists of the wrap() and fill()
methods; the other methods arejust there for
subclasses to override in order to tweak the
default behaviour.
结果为左对齐,第一行有缩进。行中的空格继续保留。
(3)denent的用法:
import textwrap text='''Object for wrapping/filling text.The public interfaceconsists of the wrap() and fill() methods; the other methods arejust there for subclasses to override in order to tweak the default behaviour.''' dedented_text = textwrap.dedent(text) print 'Dedented:' print dedented_text
输出结果为:
Dedented:
Object for wrapping/filling text.The public
interfaceconsists of the wrap() and fill() methods; the
other methods arejust there for subclasses to override in
order to tweak the default behaviour.
此时已经消除了首行的缩进了。
相关文章推荐
- mongodb搭建校内搜索引擎——爬取网页文本
- mongodb搭建校内搜索引擎——网页文本的规范化
- win10 python2.7环境下安装pypcap
- anaconda3下使用python2
- Centos 7.4.1708 安装cuda8.0、cudnn v5.1、Tensorflow 1.2.1、Python 2.7.5教程
- 64位Windows 10, 安装xgboost Python库。
- centos6.5 安装python2.7.12
- Python2.7装numpy与pandas库
- arm-linux-gcc-4.4.3交叉编译python2.7.3并移植到ARM
- 使用PyInstaller将Python程序文件转换为可执行程序文件
- 使用Python导入Excel到Sqlite3
- Python遍历目录中的所有文件的方法
- python列表生成式
- 简述Python isinstance 和type
- Python 2.7 学习笔记 中文处理
- clamav 部署
- python 文件下载 方法
- Python报错UnicodeDecodeError: ascii codec can t decode byte 0xe0 ...解决方法
- Python常用的内置序列结构(列表、元组、字典)学习笔记
- Python的作用域