Python - 重新格式化段落

简介: 行,或者在打印诗时尝试增加每一行的缩进。在本章中,我们使用名为 textwrap3 的模块根据需要格式化段落。当我们处理大量文本并将其呈现为可呈现的格式时,需要格式化段落。我们可能只想打印具有特定宽度的每一行,或者在打印诗时尝试增加每一行的缩进。
行,或者在打印诗时尝试增加每一行的缩进。在本章中,我们使用名为 textwrap3 的模块根据需要格式化段落。

当我们处理大量文本并将其呈现为可呈现的格式时,需要格式化段落。我们可能只想打印具有特定宽度的每一行,或者在打印诗时尝试增加每一行的缩进。在本章中编程字典python教程,我们使用名为 textwrap3 的模块根据需要格式化段落。

首先,我们需要安装所需的包,如下所示


pip install textwrap3

环绕固定宽度

在此示例中,我们为段落的每一行指定了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.

变量缩进

在这个例子中,我们增加了要打印的诗的每一行的缩进。


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?
目录
相关文章
|
25天前
|
索引 Python
Python中的字符串格式化:详解与应用
Python中的字符串格式化:详解与应用
20 0
|
24天前
|
Python
通过f-string编写简洁高效的Python格式化输出代码
Python 3.6中引入的f-string是Python中最常用的特征之一,它可以让我们编写更干净、更高效和更易于维护的代码,我们今天就由浅入深来详细介绍使用它的一些技巧。
89 4
|
7天前
|
Python
Python语言提供了多种输出格式化的方法,这些方法随着时间的推移和版本的更新而发展
【6月更文挑战第19天】Python格式化方法包括过时的`%`操作符,`str.format()`,推荐的f-string(Python 3.6+)和Template strings。f-string提供最佳的可读性和性能,`str.format()`是通用的,而`%`不推荐使用。模板字符串用于特定场景。对于旧版Python,使用`str.format()`或`%`。
15 4
|
6天前
|
IDE 前端开发 开发工具
怎么在isort Python 代码中的导入语句进行排序和格式化
`isort` 是一个Python工具,用于自动排序和格式化代码中的导入语句,提高代码整洁度和可读性。它支持自动排序、保留空白和注释、自定义排序规则、与多种编辑器集成以及命令行使用。安装`isort`可通过`pip install isort`,使用时可直接在Python代码中导入或通过命令行处理文件。示例展示了如何在代码中使用`isort`进行导入排序,包括基本排序、自定义设置和处理多个文件。`isort`适用于标准库、第三方库和自定义模块的导入排序,还可忽略特定导入,并能与IDE和编辑器插件集成,提升开发效率。
|
7天前
|
IDE 开发工具 开发者
isort——Python 代码中的导入语句进行排序和格式化
isort,全称是 "Import Sorting",是一个 Python 工具,用来对 Python 代码中的导入语句进行排序和格式化。它可以帮助我们按照一定的规则对导入的模块进行排序,使得代码更加整洁,易于阅读和维护。
|
11天前
|
IDE 开发工具 Python
black--一键格式化Python代码
black--一键格式化Python代码
|
4天前
Python---格式化
Python---格式化
|
27天前
|
存储 数据处理 Python
Python中一二维数据的格式化和处理技术
Python中一二维数据的格式化和处理技术
31 0
|
27天前
|
Python
Python字符串格式化
Python字符串格式化
20 0
|
1月前
|
索引 Python
Python 字符串格式化
Python 字符串格式化
17 0