python编程开发之textwrap文本样式处理技巧


Posted in Python onNovember 13, 2015

本文实例讲述了python编程开发之textwrap文本样式处理技巧。分享给大家供大家参考,具体如下:

在看python的API的时候,发现python的textwrap在处理字符串样式的时候功能强大

在这里我做了一个demo:

textwrap提供了一些方法:

wrap(text, width = 70, **kwargs):这个函数可以把一个字符串拆分成一个序列

from textwrap import *
#使用textwrap中的wrap()方法
def test_wrap():
  test_str = '''\
  The textwrap module provides two convenience functions, wrap() and fill(), as well as 1
  TextWrapper, the class that does all the work, and two utility functions, dedent() and indent(). If 2
  you're just wrapping or filling one or two text strings, the convenience functions should be good 3
  enough; otherwise, you should use an instance of TextWrapper for efficiency. 4
  '''
  print(wrap(test_str, 20))
def main():
  test_wrap()
if __name__ == '__main__':
  main()

输出效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
['  The textwrap', 'module provides two', 'convenience', 'functions, wrap()', 'and fill(), as well', 'as 1', 'TextWrapper, the', 'class that does all', 'the work, and two', 'utility functions,', 'dedent() and', 'indent(). If 2', 'you're just wrapping', 'or filling one or', 'two text strings,', 'the convenience', 'functions should be', 'good 3   enough;', 'otherwise, you', 'should use an', 'instance of', 'TextWrapper for', 'efficiency. 4']
>>>

我们会发现,wrap()函数,把字符串拆分成了一个序列,在这个序列中,每个元素的长度是一样的。

fill(text, width=70, **kwargs) :该方法可以根据指定的长度,进行拆分字符串,然后逐行显示

from textwrap import *
#fill()方法
def test_wrap():
  test_str = '''\
  The textwrap module provides two convenience functions, wrap() and fill(), as well as 1
  TextWrapper, the class that does all the work, and two utility functions, dedent() and indent(). If 2
  you're just wrapping or filling one or two text strings, the convenience functions should be good 3
  enough; otherwise, you should use an instance of TextWrapper for efficiency. 4
  '''
  print(fill(test_str, 40))
def main():
  test_wrap()
if __name__ == '__main__':
  main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
  The textwrap module provides two
convenience functions, wrap() and
fill(), as well as 1   TextWrapper,
the class that does all the work, and
two utility functions, dedent() and
indent(). If 2   you're just wrapping
or filling one or two text strings, the
convenience functions should be good 3
enough; otherwise, you should use an
instance of TextWrapper for efficiency.
>>>

dedent()方法->文本进行不缩进显示,相应的indent()方法 -> 进行缩进显示

from textwrap import *
#dedent()方法
def test_wrap():
  test_str = '''\
  The textwrap module provides two convenience
    functions, wrap() and fill(), as well as 1
  TextWrapper, the class that does all the work,
    and two utility functions, dedent() and indent(). If 2
  you're just wrapping or filling one or two text strings,
    the convenience functions should be good 3
  enough; otherwise, you should use an instance
    of TextWrapper for efficiency. 4
  '''
  print(repr(dedent(test_str)))
def main():
  test_wrap()
if __name__ == '__main__':
  main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
'The textwrap module provides two convenience\n  functions, wrap() and fill(), as well as 1\nTextWrapper, the class that does all the work,\n  and two utility functions, dedent() and indent(). If 2\nyou're just wrapping or filling one or two text strings,\n  the convenience functions should be good 3\nenough; otherwise, you should use an instance\n  of TextWrapper for efficiency. 4\n'
>>>

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
使用 Python 获取 Linux 系统信息的代码
Jul 13 Python
简述Python中的进程、线程、协程
Mar 18 Python
python利用不到一百行代码实现一个小siri
Mar 02 Python
Python实现简单文本字符串处理的方法
Jan 22 Python
Python实现的圆形绘制(画圆)示例
Jan 31 Python
Python使用OpenCV进行标定
May 08 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 Python
pytorch载入预训练模型后,实现训练指定层
Jan 06 Python
详解Python 重学requests发起请求的基本方式
Feb 07 Python
python 的topk算法实例
Apr 02 Python
python 读txt文件,按‘,’分割每行数据操作
Jul 05 Python
5 分钟读懂Python 中的 Hook 钩子函数
Dec 09 Python
python编程开发之日期操作实例分析
Nov 13 #Python
python编程开发之类型转换convert实例分析
Nov 13 #Python
python开发之文件操作用法实例
Nov 13 #Python
python开发中range()函数用法实例分析
Nov 12 #Python
python开发中module模块用法实例分析
Nov 12 #Python
Python中Class类用法实例分析
Nov 12 #Python
python开发之函数定义实例分析
Nov 12 #Python
You might like
php.ini中的request_order推荐设置
2015/05/10 PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
2016/03/05 PHP
PHP对XML内容进行修改和删除实例代码
2016/10/26 PHP
PHP单例模式应用示例【多次连接数据库只实例化一次】
2018/12/18 PHP
用js生产批量批处理执行命令
2008/07/28 Javascript
限制textbox或textarea输入字符长度的JS代码
2013/10/16 Javascript
js中Image对象以及对其预加载处理示例
2013/11/20 Javascript
jquery跟js初始化加载的多种方法及区别介绍
2014/04/02 Javascript
JavaScript中的some()方法使用详解
2015/06/09 Javascript
jQuery实现鼠标滑过点击事件音效试听
2015/08/31 Javascript
jQuery提示插件qTip2用法分析(支持ajax及多种样式)
2016/06/08 Javascript
关于原生js中bind函数的简单实现
2016/08/10 Javascript
Vue2学习笔记之请求数据交互vue-resource
2017/02/23 Javascript
解决Webpack 热部署检测不到文件变化的问题
2018/02/22 Javascript
Vue触发隐藏input file的方法实例详解
2019/08/14 Javascript
webpack打包html里面img后src为“[object Module]”问题
2019/12/22 Javascript
vue组件库的在线主题编辑器的实现思路
2020/04/03 Javascript
React.js组件实现拖拽排序组件功能过程解析
2020/04/27 Javascript
微信小程序实现倒计时功能
2020/11/19 Javascript
[02:07]TI9显影之尘系列 - Vici Gaming
2019/08/20 DOTA
[45:25]完美世界DOTA2联赛循环赛 PXG vs IO 第一场 11.06
2020/11/09 DOTA
Python open()文件处理使用介绍
2014/11/30 Python
Python实现读取邮箱中的邮件功能示例【含文本及附件】
2017/08/05 Python
Django实现单用户登录的方法示例
2019/03/28 Python
django xadmin 管理器常用显示设置方式
2020/03/11 Python
python subprocess pipe 实时输出日志的操作
2020/12/05 Python
HTML5在canvas中绘制复杂形状附效果截图
2014/06/23 HTML / CSS
印度首选时尚目的地:Reliance Trends
2018/01/17 全球购物
利用promise及参数解构封装ajax请求的方法
2021/03/24 Javascript
文明和谐家庭事迹材料
2014/05/18 职场文书
大学生村官座谈会发言材料
2014/05/25 职场文书
党的群众路线对照检查材料(个人)
2014/09/24 职场文书
2015年新教师工作总结
2015/04/28 职场文书
读书笔记怎么写
2015/07/01 职场文书
高中政治教师教学反思
2016/02/23 职场文书
Python中基础数据类型 set集合知识点总结
2021/08/02 Python