python模块学习- textwrap 文本包装和填充
来源:互联网 发布:搜游网络 融资 编辑:程序博客网 时间:2024/05/29 03:58
python模块学习- textwrap 文本包装和填充
TextWrap提供函数wrap()和fill(),以及TextWrapper类,工具函数dedent().通常包装或者填充一两个字符串使用wrap()和fill()。其他情况使用TextWrapper更高效。
textwrap.wrap(text[,width[, ...]])
包装单个段落(text为输入,系字符串),每行最长宽度width。返回输出行的列表,最后行无换行符。Width默认70。
textwrap.fill(text[,width[, ...]])
包装单段文字,并返回包含包裹段落的字符串。实际上是"\n".join(wrap(text,...))的缩写。wrap() andfill()创建TextWrapper实例,并调用一个方法。这些实例不被重用,所以包装/填充很多文本字符串要构造自己的TextWrapper对象更有效。TextWrapper.break_long_words设置是否拆长单词。
textwrap.dedent(text)
反缩进去除每行行首的空白。这方便显示三引号中的内容而不修改其源代码中的缩进。
代码实例:
sample_text = '''
'''
段落填充:
import
textwrap
from
textwrap_exampleimportsample_text
print
'Nodedent:\n'
printtextwrap.fill(sample_text, width
=
50
)
执行结果:
# pythontextwrap_fill.py
No dedent:
text for outputin
printing is desired.
functionalitysimilar to the paragraph wrapping
or fillingfeatures found in many text editors.
结果为左对齐,第一行有缩进。行中的空格继续保留。
移除缩进:
import
textwrap
fromtextwrap_example
import
sample_text
dedented_text
=
textwrap.dedent(sample_text)
print
'Dedented:'
printdedented_text
执行结果:
# pythontextwrap_dedent.py
Dedented:
The textwrapmodule can be used to format text for output in
situations wherepretty-printing is desired.
programmaticfunctionalit
or fillingfeatures found in many text editors.
这样第一行就不会缩进。
结合移除缩进和填充:
import
textwrap
fromtextwrap_example
import
sample_text
dedented_text
=
textwrap.dedent(sample_text).strip()
for
width
in
[
45
,
70
]:
print
'%dColumns:\n'
%
width
print
textwrap.fill(dedented_text,width
=
width)
print
执行结果:
# pythontextwrap_fill_width.py
45 Columns:
The textwrapmodule can be used to format
text for output insituations where pretty-
printing isdesired.
functionalitysimilar to the paragraph
wrapping orfilling features found in many
text editors.
70 Columns:
The textwrapmodule can be used to format text for output in
situations wherepretty-printing is desired.
functionality similarto the paragraph wrapping or fillingfeatures
found in many texteditors.
悬挂缩进:悬挂缩进第一行的缩进小于其他行的缩进。
import
textwrap
fromtextwrap_example
import
sample_text
dedented_text
=
textwrap.dedent(sample_text).strip()
printtextwrap.fill(dedented_text,
initial_indent
=
'',
subsequent_indent
=
''
*
4
,
width
=
50
,
)
执行结果:
# pythontextwrap_hanging_indent.py
The textwrapmodule can be usedto
format
text
for
output
in
situationswhere pretty
-
printingis
desired.It offers programmatic functionality
similarto the paragraph wrapping orfilling
featuresfound
in
manytext editors.
其中的’’*4还可以使用其他字符代替。
1.
2. pythontab技术互助群:87464755
- python模块学习- textwrap 文本包装和填充
- python2.7中模块学习- textwrap 文本包装和填充
- python textwrap模块
- [Python标准库]textwrap——格式化文本段落
- textwrap模块1-fill()
- 3.4 textwrap--格式化文本库
- 3.4 textwrap--格式化文本库(2)
- js包装模块知识点学习
- svg文本。填充和边框
- python包装和授权
- Python - 文本处理模块
- python模块学习之模块和包
- python下的mysql模块包装
- Python学习笔记---模块;文件与文本处理
- Python文本操作相关模块
- Python PIL ImageDraw 和ImageFont模块学习
- Python学习3:搜索路径和模块
- Python PIL ImageDraw 和ImageFont模块学习
- C++ string 用法详解(转)
- Timus 1017. The Staircases(转)
- hdu 1874 Dijkstra算法
- qwt编程使用(二)
- chrome设置:新标签页打开指定网页
- python模块学习- textwrap 文本包装和填充
- 跟我一起学Oracle 11g【2】----用户管理
- 美女show
- _T() 和_L() _TEXT __T,L的用法
- [C++] 对象地址与接口指针的故事
- 一些重要的算法------启发式搜索,束搜索(beam search),二分查找算法 and so on............
- 让NGUI支持超链接(Hyperlink,彩色、可点击)
- Linux 服务配置之www配置
- HDU 2433 最短路