python textwrap模块介绍

textwrap模块

该模块首先提供了三个便捷的方法:wrap,fill和decent,也提供了TextWrapper类。其中TextWrapper提供了全套的方法,前三个方法只是便捷的使用。如果只是包装或填充一个或两个文本字符串,可以直接使用便捷方法;否则,为了提高效率,应该使用TextWrapper实例。

使用

textwrap.wrap(text,[width[,…]])

该方法用text(字符串)包装单个段落,按照width的宽度进行切割,切割后返回list。
示例:

import textwrap
text = "Hi, how are you these days?  I'm glad to hear that!"
result = textwrap.wrap(text,12)
print(result)

结果:

['Hi, how are', 'you these', "days?  I'm", 'glad to hear', 'that!']

可以看出来wrap将text分割的每个list元素都是小于或等于width(这里设置的是12)的,而不是完全等于12,因为在这里wrap不会将单词截断,果加上下一个单词长度就超过width的话就不添加下一个单词。

textwrap.fill(text[, width[, …]])

fill()和上面wrap()方法类似,分割结果相同,但是返回结果形式不同,它的作用就是在分割的片段之间添加’\n’然后将其重新变成一个文本进行输出,而wrap()输出一个list
示例:

import textwrap
text = """\
This is a paragraph that already has
line breaks.  But some of its lines are much longer than the others,
so it needs to be wrapped.
Some lines are \ttabbed too.
What a mess!
"""
print('------------ after wrap------------')
wrap_result = textwrap.wrap(text, 30)
print(wrap_result)

fill_result = textwrap.fill(text, 30)
print('------------ after fill------------')
print(fill_result)

print('------------ compare --------------')
print('\n'.join(wrap_result) == fill_result)

结果:

------------ after wrap------------
['This is a paragraph that', 'already has line breaks.  But', 'some of its lines are much', 'longer than the others, so it', 'needs to be wrapped. Some', 'lines are  tabbed too. What a', 'mess!']
------------ after fill------------
This is a paragraph that
already has line breaks.  But
some of its lines are much
longer than the others, so it
needs to be wrapped. Some
lines are  tabbed too. What a
mess!
------------ compare --------------
True

textwrap.dedent(text)

这个方法可以移除不需要的前导空格。这可以用来使三引号的字符串与显示的左边缘对齐,同时仍然以缩进的形式在源代码中显示它们。
示例:

import textwrap
sample_text = '''
    hello
                my name is Jack'''

print(sample_text)
print--------after dedent---------print (textwrap.dedent(sample_text))

结果:


    hello
                my name is Jack
--------after dedent---------
hello
            my name is Jack

class textwrap.TextWrapper(…)

TextWrapper是包中的一个类,通过创建一个TextWrapper实例并在其上调用方法,wrap()、fill()和shorten()可以完成工作。该实例没有被重用,因此对于使用wrap()和fill()处理许多文本字符串的应用程序,创建自己的TextWrapper对象可能更有效。
使用:

  • 可以多次重用同一个TextWrapper对象,并且可以通过在使用之间直接分配实例属性来更改它的任何选项。
  • TextWrapper构造函数接受许多可选关键字参数,每个关键字参数对应一个实例属性,例如使用以下代码进行初始化:
wrapper = TextWrapper(initial_indent="* ")

相当于:

wrapper = TextWrapper()
wrapper.initial_indent = "* "
  • TextWrapper实例属性介绍:
    width:宽度最大值,默认是70.只要输入文本中没有单个单词超过宽度,TextWrapper就保证输出行不会超过宽度字符。
    expand_tabs:默认为True,如果设置为True,那么文本中的所有制表符都将使用text的expandtabs()方法扩展为空格。
    tabsize:(默认值:8)如果expand_tabs为真,那么文本中的所有制表符将根据当前列和给定的制表符大小扩展为零或多个空格。其实就是tab制表符大小一般为4个空格大小,如果想要将tab的的大小扩展,变成8、16、32等个空格的大小,就可以使用这个定义。
    replace_whitespace:(默认值:True)如果为真,则在展开制表符后、包装之前,wrap()方法将用单个空格替换每个空格字符(包括制表符、换行符、垂直制表符、formfeed和回车符(’\t\n\v\f\r’) )。
    drop_whitespace:(默认值:True)如果为真,则删除每行开头和结尾的空格(换行后但缩进前)。但是,如果后面没有空格,则不删除段落开头的空格。如果正在删除的空白占整行,则整行将被删除。

还有很多,不列举了,以后遇到了再了解

  • TextWrapper还提供了一些公共方法,类似于模块级的便利函数:wrap()、fill()等。

参考:
https://www.cnblogs.com/wanghui-garcia/p/10664186.html
https://blog.csdn.net/zwbzwbzwbzwbzwbzwb/article/details/52824154

你可能感兴趣的:(python基础入门,python)