📅  最后修改于: 2020-04-16 13:25:33             🧑  作者: Mango
textwrap模块可用于包装和格式化纯文本。该模块通过调整输入段落中的换行符来提供文本格式。
TextWrapper实例属性(以及构造函数的关键字参数)如下:
Textwrap模块提供的功能:
1.textwrap.wrap(text,width = 70,** kwargs):此函数包装输入的段落,使段落中的每一行最多为宽度字符。wrap方法返回输出行列表。如果包装的输出没有内容,则返回的列表为空。默认宽度为70。
import textwrap
value = """This function wraps the input paragraph such that each line
in the paragraph is at most width characters long. The wrap method
returns a list of output lines. The returned list
is empty if the wrapped
output has no content."""
# 换行.
wrapper = textwrap.TextWrapper(width=50)
word_list = wrapper.wrap(text=value)
# 打印每一行.
for element in word_list:
print(element)
输出:
This function wraps the input paragraph such that
each line in the paragraph is at most width
characters long. The wrap method returns a list of
output lines. The returned list is empty if the
wrapped output has no content.
2.textwrap.fill(text,width = 70,** kwargs): fill()便捷函数的工作方式类似于textwrap.wrap,不同之处在于它将返回的数据返回到一个用换行符分隔的单个字符串中。此函数将输入的单个段落包装为文本,并返回包含被包装的段落的单个字符串。
import textwrap
value = """This function returns the answer as STRING and not LIST."""
# 换行.
wrapper = textwrap.TextWrapper(width=50)
string = wrapper.fill(text=value)
print (string)
输出:
This function returns the answer as STRING and not LIST.
3. textwrap.dedent(text):此函数用于从输入文本的每一行中删除任何常见的前导空白。这允许使用文档字符串或嵌入的多行字符串与显示的左边缘对齐,同时删除代码本身的格式。
范例:
import textwrap
wrapper = textwrap.TextWrapper(width=50)
s = '''\
hello
world
'''
print(repr(s)) # prints ' hello\n world\n '
text = textwrap.dedent(s)
print(repr(text)) # prints 'hello\n world\n'
输出:
' hello\n world\n '
'hello\n world\n'
4,textwrap.shorten(text,width,** kwargs):此函数会截断输入字符串,以使字符串的长度等于给定的宽度。首先,通过用单个空格删除空格将所有空格折叠在字符串中。如果修改后的字符串适合给定的字符串,则返回修改后的字符串,否则将丢弃末尾的字符,以便其余单词加上占位符适合宽度。
import textwrap
sample_text = """This function wraps the input paragraph such that each line
n the paragraph is at most width characters long. The wrap method
returns a list of output lines. The returned list
is empty if the wrapped
output has no content."""
wrapper = textwrap.TextWrapper(width=50)
dedented_text = textwrap.dedent(text=sample_text)
original = wrapper.fill(text=dedented_text)
print('Original:\n')
print(original)
shortened = textwrap.shorten(text=original, width=100)
shortened_wrapped = wrapper.fill(text=shortened)
print('\nShortened:\n')
print(shortened_wrapped)
输出:
Original:
This function wraps the input paragraph such that
each line n the paragraph is at most width
characters long. The wrap method returns a list of
output lines. The returned list is empty if the
wrapped output has no content.
Shortened:
This function wraps the input paragraph such that
each line n the paragraph is at most width [...]
5,textwrap.indent(text,prefix,predicate = None):此函数用于将给定的前缀添加到所选文本行的开头。谓词参数可用于控制缩进的行。
import textwrap
s = 'hello\n\n \nworld'
s1 = textwrap.indent(text=s, prefix=' ')
print (s1)
print ("\n")
s2 = textwrap.indent(text=s, prefix='+ ', predicate=lambda line: True)
print (s2)
输出:
hello
world
+ hello
+
+
+ world