Python Text Processing 简明教程

Python - Reformatting Paragraphs

在处理大量文本并将文本呈现为可读格式时,需要对段落进行格式化。我们也许只想以特定宽度打印每一行,或在打印一首诗时尝试增加每一行的缩进。在本章中,我们将使用一个名为 textwrap3 的模块来根据需要格式化段落。

首先,我们需要按如下方式安装所需的软件包

 pip install textwrap3

Wrapping to a Fixed Width

在此示例中,我们为段落中每一行指定了 30 个字符的宽度。通过为 width 参数指定一个值来使用 wrap 函数。

from textwrap3 import wrap

text = 'In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughters wedding day.'

x = wrap(text, 30)
for i in range(len(x)):
    print(x[i])

当我们运行以上程序时,我们得到了以下输出 −

In late summer 1945, guests
are gathered for the wedding
reception of Don Vito
Corleones daughter Connie
(Talia Shire) and Carlo Rizzi
(Gianni Russo). Vito (Marlon
Brando), the head of the
Corleone Mafia family, is
known to friends and
associates as Godfather. He
and Tom Hagen (Robert Duvall),
the Corleone family lawyer,
are hearing requests for
favors because, according to
Italian tradition, no Sicilian
can refuse a request on his
daughters wedding day.

Variable Indention

在此示例中,我们增加了要打印的每首诗的缩进。

import textwrap3

FileName = ("path\poem.txt")

print("**Before Formatting**")
print(" ")

data=file(FileName).readlines()
for i in range(len(data)):
   print data[i]

print(" ")
print("**After Formatting**")
print(" ")
data=file(FileName).readlines()
for i in range(len(data)):
   dedented_text = textwrap3.dedent(data[i]).strip()
   print dedented_text

当我们运行以上程序时,我们得到了以下输出 −

**Before Formatting**

 Summer is here.
  Sky is bright.
	Birds are gone.
	 Nests are empty.
	  Where is Rain?

**After Formatting**

Summer is here.
Sky is bright.
Birds are gone.
Nests are empty.
Where is Rain?