3.4 textwrap--格式化文本库
textwrap.fill(text, width=70, **kwargs) 格式化文本,跟函数wrap的功能1样,只不过它返回的不是列表方式,而1段连接1起的文本。可以认为等价于下面的代码: “ ”.join(wrap(text, ...)) 参数与wrap参数是1样的功能。 例子: #python 3.4.3
import textwrap
text = this for test! format = textwrap.fill(text, 10) print(format)
text = You say that you love the rain, but you open your umbrella when it rains. You say that you love the sun, but you find a shadow spot when the sun shines. You say that you love the wind, but you close your windows when wind blows. This is why I am afraid, when you say that you love me too.
format = textwrap.fill(text, 30) print(format) 结果输出以下: this for test! You say that you love the rain, but you open your umbrella when it rains. You say that you love the sun, but you find a shadow spot when the sun shines. You say that you love the wind, but you close your windows when wind blows. This is why I am afraid, when you say that you love me too.
textwrap.shorten(text, width, **kwargs) 根据给出的行宽度进行格式化,当行宽度大于文本实际长度时,完全输出;当行宽度小于文本实际长度时,只输出部份头部文本,和后去掉的文本以省略号的方式显示。 参数text是要格式化的文本。 参数width是行宽度。 参数kwargs是关键字参数。 例子: #python 3.4.3
import textwrap
text = this for test! format = textwrap.shorten(text, width=12) print(format)
format = textwrap.shorten(text, width=100) print(format)
format = textwrap.shorten(text, width=10, placeholder="...") print(format) 输出结果以下: this [...] this for test! this...
textwrap.dedent(text) 把每行文本前面的缩进空格进行去除掉。 例子: #python 3.4.3
import textwrap
text = 1. this for test! 2. abc 3. shenzhen print(text) print(不要每行缩进:) format = textwrap.dedent(text) print(format) 结果输出以下: >>> 1. this for test! 2. abc 3. shenzhen 不要每行缩进: 1. this for test! 2. abc 3. shenzhen >>>
textwrap.indent(text, prefix, predicate=None) 把文本每行进行缩进,缩进前缀可使用prefix定义。 参数text是要缩进的文本。 参数prefix是缩进输出的字符串,可使用空格等等。 参数predicate是控制每行是不是缩进的lambda函数。 例子: #python 3.4.3
import textwrap
text = 1. this for test! 2. abc 3. shenzhen print(text) print(每行增加缩进:) format = textwrap.indent(text, ) print(format)
print(lambda:) format = textwrap.indent(text, 小蔡说:, lambda x: True if len(x) > 10 else False) print(format) 结果输出以下: 1. this for test! 2. abc 3. shenzhen 每行增加缩进: 1. this for test! 2. abc 3. shenzhen lambda: 小蔡说:1. this for test! 2. abc 小蔡说:3. shenzhen
class textwrap.TextWrapper(**kwargs) TextWrapper类的构造函数,构造1个处理文本格式化的对象。 参数kwargs是关键字参数,可以设置下面说明的关键字参数: width 每行最大的长度,超过此长度就进行换行。默许是70个字符。 expand_tabs 如果本标志为True,在进行文本填充操作时把跳格键使用expandtabs()函数扩大文本,反之不进行这个操作。默许是True。 tabsize 如果expand_tabs为True,在填充时按tabsize设置的大小来填充。默许为8个空格字符。 replace_whitespace 如果expand_tabs为True,此标志不起作用。如果expand_tabs为False,会把字符集合( vf )每一个替换为1个空格字符,而不作扩大。 drop_whitespace 如果此标志设置为True,在格式化之前的每行字符的行头和行尾的空格都会被删除掉。 initial_indent 在第1行的行首添加指定的字符显示。默许是空白。如果首行是空白行就不会添加。 subsequent_indent 除第1行,所有其它行都在行首添加这个字符串的输出。默许为空。 fix_sentence_endings 当此标志为True时,表示检测到句尾时添加固定两个空格在句尾,以便下1句不紧挨着上1句。默许为False。 break_long_words 当此标志为True时,如果1句话后面的单词超过设置行宽度(width),会自动切断这个单词,以便满足行宽度的长度要求。如果为False,就不切断,可能有些行就会超过行宽度的要求。 break_on_hyphens 如果此标志为True,复合词之间连字符可以视为换行的字符。如果设置为False,就不允许。默许为True。 max_lines 如果此变量定义了最多输出多少行,如果文本输出超过设置的行,就输出省略标志[...]。默许此变量为None,所有内容全部输出。 placeholder 如果文本已输出到达限制长度,就会去掉,并在此位置输出placeholder字符串。默许此字符串为[...]。用户可自己定义此字符串。 TextWrapper.wrap(text) 对1段文本进行换行等格式化,返回字符串行列表的方式。 TextWrapper.fill(text) 对1段文本进行换行等格式化返回字符串方式。
例子: #python 3.4.3
import textwrap
text = 1. this for test! 2.abc 3. shenzhen print(text) print(每行增加缩进:) txtwr = textwrap.TextWrapper(width = 10, expand_tabs = False, tabsize = 3, replace_whitespace = True, initial_indent = ###, max_lines = 2) format = txtwr.fill(text) print(format) 结果输出: 1. this for test! 2.abc 3. shenzhen 每行增加缩进: ###1. this for [...] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |