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?
目录
相关文章
|
1月前
|
存储 Python
如何在Python中读取文件的内容,并进行格式化的处理?
如何在Python中读取文件的内容,并进行格式化的处理?
41 2
|
3月前
|
Python
Python中strftime格式化时间
Python中strftime格式化时间
25 0
|
3月前
|
IDE 程序员 开发工具
Python 进阶指南(编程轻松进阶):三、使用 Black 工具来格式化代码
Python 进阶指南(编程轻松进阶):三、使用 Black 工具来格式化代码
41 0
|
1天前
|
Python
Python 字符串格式化指南
本文介绍了Python中的三种字符串格式化方法:1) 使用 `%` 操作符,如 `%s` 和 `%d`;2) `str.format()` 方法,通过 `{}` 占位符插入变量;3) Python 3.6 引入的 f-strings,直接在字符串内嵌入变量。此外,还提到了高级用法,如格式控制(如指定小数位数)。这些方法有助于更有效地处理和格式化字符串输出。
3 0
|
8天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
32 0
|
1月前
|
数据处理 Python
使用Python的time库来格式化时间
使用Python的time库来格式化时间
53 1
|
1月前
|
JSON 数据格式 Python
python写入的json文件要格式化
要将JSON格式化后写入文件,你可以在`json.dump()`函数中使用`indent`参数来设置缩进级别。以下是一个示例: ```python import json data = {"name": "John", "age": 30, "city": "New York"} with open('data.json', 'w') as file: json.dump(data, file, indent=4) ``` 在这个示例中,我们使用`json.dump()`函数将Python对象转换为JSON格式,并将其写入到文件中。通过传递`indent=4`参数,我们设置了缩
|
4月前
|
Python
python格式化输出,以及列表创建的注意事项
python格式化输出,以及列表创建的注意事项
53 1
|
5月前
|
存储 Python
python字符串的定义讲解以及格式化案例
python字符串的定义讲解以及格式化案例
|
5月前
|
JSON Linux 开发工具
linux 利用python模块实现格式化json
linux 利用python模块实现格式化json
46 0

热门文章

最新文章