textwrap:格式化文本段落
介绍
需要美观打印(pretty-printing)的情况下,可以使用textwrap模块格式化要输出的文本。
它提供了很多文本编辑器和字符处理器中都有的段落自动换行或填充特性
填充段落
import textwrap
text = '''
There are moments in life when you miss someone
so much that you just want to pick them
from your dreams and hug them for real! Dream what
you want to dream;go where you want to go;
be what you want to be,because you have
only one life and one chance to do all the things you want to do.
'''
# fill函数取文本作为输入,生成格式化文本作为输出
# width表示宽度为50
print(textwrap.fill(text, width=50))
'''
There are moments in life when you miss
someone so much that you just want to pick
them from your dreams and hug them for real!
Dream what you want to dream;go where you
want to go; be what you want to be,because you
have only one life and one chance to do all
the things you want to do.
'''
结果不是太让人满意。文本虽然已经对齐,不过只有第一行保留了缩进,后面各行的空格则嵌入在段落中
去除现有的缩进
import textwrap
text = '''
There are moments in life when you miss someone
so much that you just want to pick them
from your dreams and hug them for real! Dream what
you want to dream;go where you want to go;
be what you want to be,because you have
only one life and one chance to do all the things you want to do.
'''
# 关于刚才的例子,其输出中混合嵌入了制表符和额外的空格,所以格式不是太美观。
# 用dedent可以去除示例文本中所有的行前面的空白符,这会生成更好的结果
# 并且允许在Python代码中直接使用docstring或者内嵌的多行字符串,同时去除代码本身的格式。
print(textwrap.dedent(text).strip())
'''
There are moments in life when you miss someone
so much that you just want to pick them
from your dreams and hug them for real! Dream what
you want to dream;go where you want to go;
be what you want to be,because you have
only one life and one chance to do all the things you want to do.
'''
可以看到dedent作用就是把每一行开头的缩进给去掉
结合dedent和fill
import textwrap
text = '''
There are moments in life when you miss someone
so much that you just want to pick them
from your dreams and hug them for real! Dream what
you want to dream;go where you want to go;
be what you want to be,because you have
only one life and one chance to do all the things you want to do.
'''
# 先去除缩进,然后填充,每行长度60个字符
dedent_text = textwrap.dedent(text).strip()
print(textwrap.fill(dedent_text, width=60))
'''
There are moments in life when you miss someone so much
that you just want to pick them from your dreams and hug
them for real! Dream what you want to dream;go where you
want to go; be what you want to be,because you have only
one life and one chance to do all the things you want to do.
'''
缩进块
import textwrap
text = '''
There are moments in life when you miss someone
so much that you just want to pick them
from your dreams and hug them for real! Dream what
you want to dream;go where you want to go;
be what you want to be,because you have
only one life and one chance to do all the things you want to do.
'''
# 可以使用indent函数为一个字符串的所有行增加一致的前缀文本
dedent_text = textwrap.dedent(text).strip()
final_text = textwrap.indent(dedent_text, ">>>")
print(final_text)
'''
>>>There are moments in life when you miss someone
>>>so much that you just want to pick them
>>>from your dreams and hug them for real! Dream what
>>>you want to dream;go where you want to go;
>>>be what you want to be,because you have
>>>only one life and one chance to do all the things you want to do.
'''
# 除此之外还可以给指定行添加
final_text = textwrap.indent(dedent_text, prefix="->", predicate=lambda line: len(line.strip()) > 40)
print(final_text)
'''
->There are moments in life when you miss someone
so much that you just want to pick them
->from your dreams and hug them for real! Dream what
->you want to dream;go where you want to go;
be what you want to be,because you have
->only one life and one chance to do all the things you want to do.
'''
# 显然lambda里面的line就是每一行的文本,指定文本长度大于40的
悬挂缩进
import textwrap
text = '''
There are moments in life when you miss someone
so much that you just want to pick them
from your dreams and hug them for real! Dream what
you want to dream;go where you want to go;
be what you want to be,because you have
only one life and one chance to do all the things you want to do.
'''
# 不仅可以设置输出的宽度,还可以采用同样的方式单独控制首行的缩进,使首行的缩进不同于后续的各行
dedent_text = textwrap.dedent(text).strip()
print(textwrap.fill(dedent_text,
initial_indent="",
subsequent_indent=" "*4,
width=50))
'''
There are moments in life when you miss someone
so much that you just want to pick them from
your dreams and hug them for real! Dream what
you want to dream;go where you want to go; be
what you want to be,because you have only one
life and one chance to do all the things you
want to do.
'''
# 这便可以生成悬挂缩进,即首行缩进小于其他行的缩进
# 缩进值也可以包含非空白字符。
print(textwrap.fill(dedent_text,
initial_indent="", # 首行缩进
subsequent_indent="*"*4, # 首行之外的行缩进
width=50))
'''
There are moments in life when you miss someone
****so much that you just want to pick them from
****your dreams and hug them for real! Dream what
****you want to dream;go where you want to go; be
****what you want to be,because you have only one
****life and one chance to do all the things you
****want to do.
'''
截断长文本
import textwrap
text = '''
There are moments in life when you miss someone
so much that you just want to pick them
from your dreams and hug them for real! Dream what
you want to dream;go where you want to go;
be what you want to be,because you have
only one life and one chance to do all the things you want to do.
'''
dedent_text = textwrap.dedent(text).strip()
print(textwrap.shorten(dedent_text, width=50)) # There are moments in life when you miss [...]
textwrap:格式化文本段落的更多相关文章
- WPF 语言格式化文本控件
前言 本章讲述正确添加语言资源的方式,以及一段语言资源的多种样式显示. 例如:“@Winter,你好!感谢已使用软件 800 天!” 在添加如上多语言资源项时,“XX,你好!感谢已使用软件 X 天!” ...
- jQuery文本段落展开和折叠效果
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...
- 解决方案:带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载可能出现 COM 组件的80040154错误
建议大家在微软的组件出现问题时,在GOOGLE上搜索解决方案,一般来说,总有结果: 带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载,可能出现 COM 组件的80 ...
- python中用xpath匹配文本段落内容的技巧
content = item.xpath('//div[@class="content"]/span')[0].xpath('string(.)') content = item. ...
- c# 正则格式化文本防止SQL注入
/// <summary> /// 格式化文本(防止SQL注入) /// </summary> /// <param name="str">&l ...
- python中使用%与.format格式化文本
初学python,看来零零碎碎的格式化文本的方法,总结一下python中格式化文本的方法.使用不当的地欢迎指出谢谢. 1.首先看使用%格式化文本 常见的占位符: 常见的占位符有: %d 整数 %f 浮 ...
- 格式化文本数据抽取工具awk
在管理和维护Linux系统过程中,有时可能需要从一个具有一定格式的文本(格式化文本)中抽取数据,这时可以使用awk编辑器来完成这项任务.发明这个工具的作者是Aho.Weinberg和Kernighan ...
- jQuery 文本段落展开和折叠效果
jQuery 文本段落展开和折叠效果 <!DOCTYPE html> <head> <meta http-equiv="Content-Type" c ...
- 【python cookbook】【字符串与文本】16.以固定的列数重新格式化文本
问题:重新格式化一些很长的字符串,以指定的列数来显示 解决方案:textwrap模块的fill()方法来实现 # A long string s = "Look into my eyes, ...
随机推荐
- flask(1.1)装饰器装饰多个视图函数出现的问题
目录 1.装饰器装饰多个视图函数出现的问题 2.使用装饰器修复技术解决该问题 1.装饰器装饰多个视图函数出现的问题 代码实例: from flask import Flask, request, re ...
- uni-app 使用本地打包配置安卓原生插件
在使用 uni-app 开发的时候,遇到了一个很棘手的问题.即获取设备参数的时候 uni-app 并没有相关方法,而安卓开发是可以做到的,因为接的是三方推广,所以功能必须实现,所以求助了安卓的大佬帮我 ...
- 2.React 生命周期函数
什么是生命周期函数:在某一时刻组件会自动调用执行的函数. import React,{ Component,Fragment } from 'react' class Note extends Com ...
- Linux特殊权限位suid、sgid深度详细及实践
特殊权限位基本说明: Linux系统基本权限位为9位权限,但还有额外3位权限位,共12位权限: suid s(有x) S 4 用户对应的权限位(用户对应的3位上) sgid ...
- PTA (Advanced Level)1077.Kuchiguse
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
- java知识随笔整理-Oracle存储过程优缺点
优点: 1.存储过程可以使得程序执行效率更高.安全性更好. 2.建立过程不会很耗系统资源,因为过程只是在调用才执行. 3.存储过程可以用于降低网络流量,存储过程代码直接存储于数据库中,所以不会产生大量 ...
- apache tika检测文件是否损坏
Apache Tika用于文件类型检测和从各种格式的文件内容提取的库. 将上传文件至服务器,进行解析文件时,经常需要判断文件是否损坏.我们可以使用tika来检测文件是否损坏 maven引入如下: &l ...
- 解决连接HIS连接不上数据库的问题
运行程序单步运行,设置断点 配置HIS中的 GetDataBaseInfo类,将与本机无关的配置函数全部删除,(按照DMHospital.ini文件来对照修改),如下图: 往数据库中所创建的表中添加数 ...
- [ZJOI2010]数字计数 题解
题面 这道题是一道数位DP的模板题: 因为窝太蒟蒻了,所以不会递推,只会记忆化搜索: 首先,咋暴力咋来: 将一个数分解成一个数组,这样以后方便调用: 数位DP的技巧:(用1~b的答案)-(1~a的答案 ...
- [AHOI2017初中组]guide 题解
题面 我们无论怎么走,都是要从此点沿最短路径走到终点,所以我们以n为原点跑两边dijkstra就可以了: 而抱怨数可以根据之前跑出来的东西新建一个图,然后跑最短路就好了: #include <b ...