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执行子进程实现进程间通信的方法
Jun 02 Python
python简单分割文件的方法
Jul 30 Python
Python中矩阵库Numpy基本操作详解
Nov 21 Python
Python matplotlib画图实例之绘制拥有彩条的图表
Dec 28 Python
PyCharm代码整体缩进,反向缩进的方法
Jun 25 Python
详解基于django实现的webssh简单例子
Jul 17 Python
浅谈Python中的bs4基础
Oct 21 Python
python 执行文件时额外参数获取的实例
Dec 18 Python
对Python 语音识别框架详解
Dec 24 Python
python按照多个条件排序的方法
Feb 08 Python
关于Pytorch的MNIST数据集的预处理详解
Jan 10 Python
Django框架中视图的用法
Jun 10 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
laradock环境docker-compose操作详解
2019/07/29 PHP
使一个函数作为另外一个函数的参数来运行的javascript代码
2007/08/13 Javascript
DOM下的节点属性和操作小结
2009/05/14 Javascript
用js脚本控制asp.net下treeview的NodeCheck的实现代码
2010/03/02 Javascript
jquery关于图形报表的运用实现代码
2011/01/06 Javascript
十个迅速提升JQuery性能让你的JQuery跑得更快
2012/12/10 Javascript
JS文本框默认值处理详解
2013/07/10 Javascript
jQuery extend 的简单实例
2013/09/18 Javascript
javascript 密码框防止用户粘贴和复制的实现代码
2014/02/17 Javascript
JS中产生20位随机数以0-9为例也可以是a-z A-Z
2014/08/01 Javascript
html5+javascript实现简单上传的注意细节
2016/04/18 Javascript
微信小程序实现电子签名并导出图片
2020/05/27 Javascript
JS如何操作DOM基于表格动态展示数据
2020/10/15 Javascript
[02:43]DOTA2英雄基础教程 德鲁伊
2014/01/13 DOTA
[45:18]2018DOTA2亚洲邀请赛 4.3 突围赛 Optic vs iG 第一场
2018/04/04 DOTA
[58:54]EG vs RNG 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
python基于mysql实现的简单队列以及跨进程锁实例详解
2014/07/07 Python
python自动化测试之连接几组测试包实例
2014/09/28 Python
Python中实现变量赋值传递时的引用和拷贝方法
2018/04/29 Python
基于python if 判断选择结构的实例详解
2019/05/06 Python
python selenium自动化测试框架搭建的方法步骤
2020/06/14 Python
Python新手学习标准库模块命名
2020/05/29 Python
详解Python中string模块除去Str还剩下什么
2020/11/30 Python
行政人事经理职位说明书
2014/03/05 职场文书
总经理秘书岗位职责
2014/03/17 职场文书
施工安全承诺书
2014/05/22 职场文书
制冷与空调专业毕业生推荐信
2014/07/07 职场文书
开业庆典活动策划方案
2014/09/21 职场文书
教师个人自我剖析材料
2014/09/29 职场文书
组织生活会发言材料
2014/12/15 职场文书
2015年综治宣传月活动总结
2015/03/25 职场文书
学习雷锋精神倡议书
2015/04/27 职场文书
2016年六一文艺汇演开幕词
2016/03/04 职场文书
2016年小学“公民道德宣传日”活动总结
2016/04/01 职场文书
react如何快速设置文件路径别名
2021/04/28 Javascript
如何用Node.js编写内存效率高的应用程序
2021/04/30 Javascript