跳转至

6.2.textwrap

Windows 10
Python 3.7.3 @ MSC v.1915 64 bit (AMD64)
import textwrap
from toolkit.Help import Help as H

删除前导空格和后缀空格

删除第一行的前导空格和最后一行的后缀空格,width参数控制每一行的宽度:

sample_text = '''
    The textwrap module can be used to format text for output in
    situations where pretty-printing is desired.  It offers
    programmatic functionality similar to the paragraph wrapping
    or filling features found in many text editors.
    '''

print(sample_text)
print(textwrap.fill(sample_text, width=50))
    The textwrap module can be used to format text for output in
    situations where pretty-printing is desired.  It offers
    programmatic functionality similar to the paragraph wrapping
    or filling features found in many text editors.

     The textwrap module can be used to format
text for output in     situations where pretty-
printing is desired.  It offers     programmatic
functionality similar to the paragraph wrapping
or filling features found in many text editors.

结果并不尽如人意。文本现在是左对齐的,只有第一行保留了缩进,但是原来的每一行的末尾和下一行的开头之间仍有空格。

移除所有空格

使用 dedent() 函数可以移去所有行中的空格前缀和后缀:

dedented_text = textwrap.dedent(sample_text)
print(dedented_text)
The textwrap module can be used to format text for output in
situations where pretty-printing is desired.  It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.

因为 dedentindent 的反义词, 所以输出结果是一段删除了每一行中都存在的缩进空白的文字。如果某一行比其他行缩进的更多,多出的部分将不会被移除。

print(textwrap.dedent("""
 Line one.
  Line two.
   Line three.
"""))
Line one.
 Line two.
  Line three.

添加前缀

indent() 函数在字符串每一行开头加入前缀文本。这个例子非常类似电子邮件回复中被引用的部分,使用 > 符号来做每行文字的前缀。

dedented_text = textwrap.dedent(sample_text)
wrapped = textwrap.fill(dedented_text, width=50)
wrapped += '\n\nSecond paragraph after a blank line.'
print(wrapped)
final = textwrap.indent(wrapped, '> ')
print('Quoted block:\n')
print(final)
 The textwrap module can be used to format text
for output in situations where pretty-printing is
desired.  It offers programmatic functionality
similar to the paragraph wrapping or filling
features found in many text editors.

Second paragraph after a blank line.
Quoted block:

>  The textwrap module can be used to format text
> for output in situations where pretty-printing is
> desired.  It offers programmatic functionality
> similar to the paragraph wrapping or filling
> features found in many text editors.

> Second paragraph after a blank line.

一段文字被分成了几行,每一行文字前都加了前缀,然后每行文字重新组成整个文字段落并返回。

要控制特定的一行接受新前缀,给 indent()predicate 参数赋值。该操作会轮流遍历每行的文本,当值为真时将在该行加上前缀。

def should_indent(line):
    print('Indent {!r}?'.format(line))
    return len(line.strip()) % 2 == 0

dedented_text = textwrap.dedent(sample_text)
wrapped = textwrap.fill(dedented_text, width=50)
final = textwrap.indent(wrapped, 'EVEN ',
                        predicate=should_indent)

print('\nQuoted block:\n')
print(final)
Indent ' The textwrap module can be used to format text\n'?
Indent 'for output in situations where pretty-printing is\n'?
Indent 'desired.  It offers programmatic functionality\n'?
Indent 'similar to the paragraph wrapping or filling\n'?
Indent 'features found in many text editors.'?

Quoted block:

EVEN  The textwrap module can be used to format text
for output in situations where pretty-printing is
EVEN desired.  It offers programmatic functionality
EVEN similar to the paragraph wrapping or filling
EVEN features found in many text editors.

悬挂缩进

同时也可以设置输出段落的宽度,可以单独控制首行的缩进。

dedented_text = textwrap.dedent(sample_text).strip()
print(textwrap.fill(dedented_text,
                    initial_indent='',
                    subsequent_indent=' ' * 4,
                    width=50,
                    ))
The textwrap module can be used to format text for
    output in situations where pretty-printing is
    desired.  It offers programmatic functionality
    similar to the paragraph wrapping or filling
    features found in many text editors.

减短长文本

为了查看长文本的摘要或预览,可以使用 shorten() 。所有的空格,比如制表符、换行符以及一系列的空格都将标准化为单个空格。然后此文本将减短为要求的长度来显示,在字词边界之间,将不包括不完整的词。

dedented_text = textwrap.dedent(sample_text)
original = textwrap.fill(dedented_text, width=50)

print('Original:\n')
print(original)

shortened = textwrap.shorten(original, 100)
shortened_wrapped = textwrap.fill(shortened, width=50)

print('\nShortened:\n')
print(shortened_wrapped)
Original:

 The textwrap module can be used to format text
for output in situations where pretty-printing is
desired.  It offers programmatic functionality
similar to the paragraph wrapping or filling
features found in many text editors.

Shortened:

The textwrap module can be used to format text for
output in situations where pretty-printing [...]

如果非空字元在原文本中被当作减短的部分被移除,他将替换为占位符。默认值 [...] 可以被替换,在 shorten() 中加入 placeholder 参数。

强制换行

每一行超过20个字符就强制换行(可能会导致单词被拆分):

textwrap.wrap(sample_text, width=20)
['     The textwrap',
 'module can be used',
 'to format text for',
 'output in',
 'situations where',
 'pretty-printing is',
 'desired.  It offers',
 'programmatic',
 'functionality',
 'similar to the',
 'paragraph wrapping',
 'or filling features',
 'found in many text',
 'editors.']