python标准库textwrap

textwrap

通过调整换行符在段落中出现的位置来格式化文本。允许通过编程提供类似段落自动换行或填充特性等功能。

一、填充段落

用fill函数获取文本输入,然后生成格式化的文本输出。将成段的文本储存在sample_text中,通过fill函数中的width参数控制输出文本段落的宽度。

import textwrap

sample_text="""

   Failure is probably the fortification in your pole.
   It is like a peek your wallet as the thief,
when you are thinking how to spend several hard-won lepta,
when you are wondering whether new money, it has laid background.
Because of you, then at the heart of the most lax,
alert, and most low awareness, and left it godsend failed.


"""

print("no depent:\n")
print(textwrap.fill(sample_text,width=50))

运行结果

no depent:

     Failure is probably the fortification in your
pole.    It is like a peek your wallet as the
thief, when you are thinking how to spend several
hard-won lepta, when you are wondering whether new
money, it has laid background. Because of you,
then at the heart of the most lax, alert, and most
low awareness, and left it godsend failed.

运行结果,文本左对齐,第一行做了缩进,注意,每行前的空格也会嵌入段落中。

二、去除缩进

利用textwrap中的dedent函数来去除每行前的一个缩进,第一行前有两个空格,第二,三,四行前有一个空格。用indent函数来增加缩进。

import textwrap

sample_text="""
  Failure is probably the fortification in your pole.
 It is like a peek your wallet as the thief,
 when you are thinking how to spend several hard-won lepta,
 when you are wondering whether new money, it has laid background.
  Because of you, then at the heart of the most lax,
 alert, and most low awareness, and left it godsend failed.
"""


print("depent:\n")
print(textwrap.dedent(sample_text))

运行结果

第一行前的空格为一个,第二,三,四行前没有空格。

depent:


 Failure is probably the fortification in your pole.
It is like a peek your wallet as the thief,
when you are thinking how to spend several hard-won lepta,
when you are wondering whether new money, it has laid background.
 Because of you, then at the heart of the most lax,
alert, and most low awareness, and left it godsend failed.

三、wrap

wrap(text, width = 70, **kwargs):这个函数可以把一个字符串拆分成一个序列

import textwrap

sample_text="""
  Failure is probably the fortification in your pole.
 It is like a peek your wallet as the thief,
 when you are thinking how to spend several hard-won lepta,
 when you are wondering whether new money, it has laid background.
  Because of you, then at the heart of the most lax,
 alert, and most low awareness, and left it godsend failed.
"""


print("depent:\n")
print(textwrap.wrap(sample_text,20))

运行结果

depent:

['   Failure is', 'probably the', 'fortification in', 'your pole.  It is', 'like a peek your', 'wallet as the thief,', 'when you are', 'thinking how to', 'spend several hard-', 'won lepta,  when you', 'are wondering', 'whether new money,', 'it has laid', 'background.', 'Because of you, then', 'at the heart of the', 'most lax,  alert,', 'and most low', 'awareness, and left', 'it godsend failed.']

拆分之后的文本,每个字符串的长度都是一样的,长度20。

四、悬挂缩进

用fill函数控制输出的宽度,用initial_indent控制首行的缩进,用subsequent_indent控制其他行的缩进。

import textwrap

sample_text="""
  Failure is probably the fortification in your pole.
 It is like a peek your wallet as the thief,
 when you are thinking how to spend several hard-won lepta,
 when you are wondering whether new money, it has laid background.
  Because of you, then at the heart of the most lax,
 alert, and most low awareness, and left it godsend failed.
"""

dedented_text=textwrap.dedent(sample_text).strip()

print(textwrap.fill(dedented_text,initial_indent=' ',subsequent_indent=' '*4,width=50,))

运行结果

 Failure is probably the fortification in your
    pole. It is like a peek your wallet as the
    thief, when you are thinking how to spend
    several hard-won lepta, when you are wondering
    whether new money, it has laid background.
    Because of you, then at the heart of the most
    lax, alert, and most low awareness, and left
    it godsend failed.

还可以修改缩进的符号

print(textwrap.fill(dedented_text,initial_indent='*',subsequent_indent='*'*4,width=50,))

 

你可能感兴趣的:(python标准库)