Python文本换行和填充

2025年1月5日 | 阅读 7 分钟

引言

在本教程中,我们将学习 Python 中的文本换行与填充。Textwrap 模块可用于纯文本的换行和格式化。该模块通过纠正输入句子的换行来提供文本格式化。为了使用这些模块,我们需要在代码中导入 textwrap 模块。命令如下:

TextWrapper 实例的属性和构造函数的关键字参数如下:

a. width

width 指的是允许换行的最大长度。width 的默认值设置为 70。

b. drop_whitespace

drop_whitespace 的默认值设置为 TRUE。如果此值设置为 TRUE,则会删除每行开头和结尾之间的空格(换行后,缩进前)。

c. initial_indent

initial_indent 的默认值设置为 ' '。此方法将给定字符串添加到输出换行的第一行。

d. subsequent_indent

subsequent_indent 的默认值设置为 ' '。此方法将给定字符串添加到换行的所有行,但第一行除外。

e. expand_tabs

expand_tabs 的默认值设置为 TRUE。如果此值设置为 true,则使用此方法扩展示例输入字段中的所有制表符。

f. tabsize

tabsize 的默认值设置为 8。此方法将文本中的所有制表符扩展为零个或多个空格。如果 expand_tabs 的计算结果为 TRUE,则取决于当前列和制表符大小。

g. replace_whitespace

replace_whitespace 的默认值设置为 TRUE。如果为 true,则 wrap() 方法在制表符扩展后、换行前用空格替换每个空格字符。在 Python 中,制表符(t)、换行符(n)、垂直制表符(v)、换页符(f)和回车符(r)定义的空格字符被替换为 '\t\n\v\f\r'。

h. placeholder

placeholder 的默认值设置为 "[...]"。如果字符串被截断,此方法会将字符串附加到输出的末尾。

i. max_lines

max_lines 的默认值设置为 "None"。如果值不是 None,则输出最多包含 max_lines,并在输出末尾添加空格。

j. break_long_words

break_long_words 的默认值设置为 True。如果为 true,则将长度超过 width 的单词拆分以适应给定 width 的每一行。如果值为 FALSE,则不会截断长消息,而是将其放在单独的行上以最大程度地减少溢出。

k. break_on_hyphens

break_on_hyphens 的默认值设置为 True。如果此值设置为 TRUE,则在复合词中的空格之间以及破折号后发生换行。当值为 FALSE 时,换行仅出现在中间,但如果您希望单词不可见,则必须将 break_long_words 设置为 FALSE。

Python Textwrap 模块的方法

Python Textwrap 模块有一些方法,如下所示:

1. wrap(text, width=70, **kwargs)

此函数换行输入字符串,使每行字符数不超过 width。wrap 方法返回输出行的列表。如果 wrapper 不包含内容,则返回空表。总体平均宽度为 70。

程序代码

这里我们给出 textwrap.wrap(text, width=70, **kwargs) 函数用于在 Python 中换行文本的程序代码。代码如下:

输出

现在,我们运行上面的代码,并通过换行文本找到给定值的每一行。输出如下:

This function is used to wraps the input string so
that each line in the string is one long-width
character. The wrap method returns the list of
output lines. If there is no content in the
wrapper, then the returned form is empty. The
overall average width is 70.

2. fill(text, width = 70, **kwargs)

简单的 fill() 函数的功能与 textwrap.wrap 类似,只是它返回连接后的数据到一个新扩展的字符串。此函数换行输入文本字符串并返回包含换行字符串的字符串。

程序代码

这里我们给出 textwrap.fill(text, width=70, **kwargs) 函数用于在 Python 中换行和填充文本的程序代码。代码如下:

输出

现在,我们运行上面的代码,并通过换行文本找到给定值的每一行。输出如下:

The simple fill() function works like
textwrap.wrap except it returns the concatenated
data into a new expanded string.

3. dedent(text)

此函数删除每行输入的所有前导空格。这允许使用 docstrings 或嵌入的左对齐多行字符串,同时删除格式化本身的数字。

程序代码

这里我们给出 textwrap.dedent(text) 函数用于在 Python 中换行和填充文本的程序代码。代码如下:

输出

现在,我们运行上面的代码,并通过换行文本找到给定值的每一行。输出如下:

'\\ \n    Hello \n      coders \n    '
'\\ \n    Hello \n      coders \n'

4. shorten(text, width, **kwargs)

textwrap.shorten() 函数用于截断输入字符串,使其长度等于 width。首先,通过删除空格来拆分字符串中的每个空格。如果替换后的字符串与给定字符串匹配,则返回该字符串;否则,将删除最后几个字符,从而将剩余的消息附加到空格以使其符合 width。

程序代码

这里我们给出 textwrap.shorten(text, width, **kwargs) 函数用于在 Python 中换行和填充文本的程序代码。代码如下:

输出

现在,我们运行上面的代码,并通过换行文本找到给定值的每一行。输出如下:

The original string is:

The textwrap.shorten() function is used to
truncate the input string so that its length
equals the width. Firstly, split each space in the
string by removing the whitespace with a space. It
is returned if the replaced string matches the
given string, otherwise the last characters are
removed and thus the rest of the message is
appended to the space to fit within the width.

The shortened string is:

The textwrap.shorten() function is used to
truncate the input string so that its length
equals [...]

5. indent(text, prefix, predicate = None)

此函数用于在选定列的开头添加前缀。Predicate 参数可用于控制哪些行需要缩进。

程序代码

这里我们给出 textwrap.indent(text, prefix, predicate = None) 函数用于在 Python 中换行和填充文本的程序代码。代码如下:

输出

现在,我们运行上面的代码,并通过换行文本找到给定值的每一行。输出如下:

Hello

 
 Coders


* Hello
* 
*  
* Coders

结论

在本教程中,我们学习了 Python 中的文本换行与填充。我们还学习了文本换行和填充的各种方法以及示例。