textwrap库
textwrap库是什么?
对字体有较高要求时,该库可以格式化文本,提供了许多类似文本编辑器和文本处理器中拥有的整段覆盖或填充特性的编程功能。
textwrap.wrap(text, width=70, **kwargs)
在文本(字符串)中包装单个段落,每行最大宽度为width。返回不包含最终换行符的输出行列表。
width默认为70。
import textwrap
text = '''Knowing that they are studying some degree subjects in English in the following years, these 3 students are eager to improve their academic skills. After consulting with their English teachers, they choose this course to learn and practice their academic skills through a practical project, that is, they are required to write an academic essay and give a related presentation based on one set theme at the end of this semester. Though having high expectations, there will be a lot to learn during the process of fulfilling the tasks.
At the first sight of the tasks, the 3 students feel overwhelmed. So our first and primary focus is to help them unpack the tangled web of academic demands and to bulit their confidence by informing them of the importance of having communication with their tutors and classmates during the whole learning process. In case of any doubt or trouble apart from autonomous learning, they can choose email or face-to-face talk with their tutors, and also will be helpful to discuss with their groups and classmates.
'''
res = textwrap.wrap(text)
print(type(res))
for i in res:
print(i)
textwrap.fill(text, width=70, **kwargs)
将text中的每个段落单独包装,并返回包含已包装段落的单个字符串。
import textwrap
text = '''Knowing that they are studying some degree subjects in English in the following years, these 3 students are eager to improve their academic skills. After consulting with their English teachers, they choose this course to learn and practice their academic skills through a practical project, that is, they are required to write an academic essay and give a related presentation based on one set theme at the end of this semester. Though having high expectations, there will be a lot to learn during the process of fulfilling the tasks.
At the first sight of the tasks, the 3 students feel overwhelmed. So our first and primary focus is to help them unpack the tangled web of academic demands and to bulit their confidence by informing them of the importance of having communication with their tutors and classmates during the whole learning process. In case of any doubt or trouble apart from autonomous learning, they can choose email or face-to-face talk with their tutors, and also will be helpful to discuss with their groups and classmates.
'''
res = textwrap.fill(text)
print(type(res))
print(res)
textwrap.shorten(text, width, **kwargs)
将text收缩截取到给定的width。
import textwrap
res1 = textwrap.shorten("Hello world!", width=12)
res2 = textwrap.shorten("Hello world!", width=11)
res3 = textwrap.shorten("Hello world", width=10, placeholder="...")
print(res1)
print(res2)
print(res3)
textwrap.dedent(text)
从text的每一行中删除任何常见的前导空格。
这可以用来使三重引号的字符串与显示的左边缘对齐,同时仍然以缩进的形式在源代码中显示它们。
import textwrap
def test():
# end first line with \ to avoid the empty line!
s = '''\
hello
world
'''
print(repr(s)) # prints ' hello\n world\n '
print(repr(textwrap.dedent(s))) # prints 'hello\n world\n'
if __name__ == '__main__':
test()
textwrap.indent(text, prefix, predicate=None)
将prefix添加到text中所选行的开头。
行通过调用text.splitlines(True)来分隔。
默认情况下,prefix被添加到所有不完全由空格组成的行(包括任何结束符行)。
import textwrap
s = 'hello\n\n \nworld'
print(s)
print(textwrap.indent(s, ' '))
print(textwrap.indent(s, '+ ', lambda line: True))
类 textwrap.TextWrapper(**kwargs)
实例化类的两种方法
import textwrap
# 第一种
wrapper = textwrap.TextWrapper(initial_indent="* ")
# 第二种
wrapper = textwrap.TextWrapper()
wrapper.initial_indent = "* "
参数
1. width
默认:70。每行的最大宽度。
2. expand_tabs
默认:True。如果为True,text中所有的制表符都被扩展为空格。
3. tabsize
默认:8。如果expand_tabs为True,则text中的制表符将被扩展为0或更多个空格。
4. replace_whitespace
默认:True。如果为True,在制表符展开之后但包装之前,wrap()方法将用单个空格替换每个空格字符
5. drop_whitespace
默认:True。如果为True,每行的开头和结尾的空格(在包装之后但在缩进之前)将被删除。但是,如果在段落的开头有非空格,则不删除该段落的开头的空格。如果正在删除的空白占据整行,则整行将被删除。
6. initial_indent
默认:’ '。添加到包装后每行输出前的字符串。
7. subsequent_indent
默认:‘ ’。添加到包装后每行输出(除第一行外)前的字符串。
8. fix_sentence_endings
默认:False。TextWrapper试图检测句子的结尾,并确保句子总是被两个空格隔开。
9. break_long_words
默认:True。如果为True,可以将超过width的单词分开。
10. break_on_hyphens
默认:True。如果为True,在复合词中,最好在空格和连字符后面进行换行。如果为False,则只会在空格处换行。
11. max_lines
默认:None。如果不为None,将输出max_lines行结果。
12. placeholder
默认: ’ […]'。如果使用了折叠方法,该字符串将添加到输出文本的结尾。