Python - 重新格式化段落
当我们处理大量文本并将其呈现为可呈现的格式时,需要格式化段落。我们可能只想打印具有特定宽度的每一行,或者在打印诗时尝试增加每一行的缩进。在本章中编程字典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?
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
php Laravel 数据库之:数据库请求构建器
Laravel 数据库之:数据库请求构建器 简介 Laravel 的数据库查询构造器提供了一个方便的接口来创建及运行数据库查询语句。它能用来执行应用程序中的大部分数据库操作,且能在所有被支持的数据库系统中使用。 Laravel 的查询构造器使用 PDO 参数绑定来保护你的应用程序免受 SQL 注入的攻击。因此没有必要清理作为绑定传递的字符串。 获取结果 从数据表中获取所有的数据 你可以在 DB facade 上使用 table 方法开始查询。这个 table 方法为给定的表返回一个查询构造器实例,允许你在查询上链式调用更多的约束,最后使用 get 方法获取最终结果: <?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; class UserController extends Controller { /** * 显示所有应用程序用户的列表 * * @return Response */ public functio...
- 下一篇
Python零基础学习笔记(七)—— Number数字类型及其转换
Number数字类型整型:定义变量的方法 直接法 num1 = 1 num2 = num1 连续 num1 = num2 = 1 交互式赋值 num1 num2 = 1,2 浮点型(小数):由整数部分和小数部分组成num1 = 1.1 复数:实数不分和虚数部分组成 a + bj 数字类型之间的转换如下图转换结果如下图
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Mario游戏-低调大师作品
- 2048小游戏-低调大师作品
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- 设置Eclipse缩进为4个空格,增强代码规范
- Windows10,CentOS7,CentOS8安装Nodejs环境
- MySQL8.0.19开启GTID主从同步CentOS8
- Docker快速安装Oracle11G,搭建oracle11g学习环境