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实现跨文件全局变量的方法
Jul 07 Python
为Python的web框架编写前端模版的教程
Apr 30 Python
浅谈Python中的数据类型
May 05 Python
Python基础入门之seed()方法的使用
May 15 Python
python中defaultdict的用法详解
Jun 07 Python
Python爬虫实现爬取京东手机页面的图片(实例代码)
Nov 30 Python
对python中数据集划分函数StratifiedShuffleSplit的使用详解
Dec 11 Python
Python后台开发Django会话控制的实现
Apr 15 Python
python3 pillow模块实现简单验证码
Oct 31 Python
mac在matplotlib中显示中文的操作方法
Mar 06 Python
python下对hsv颜色空间进行量化操作
Jun 04 Python
Pycharm制作搞怪弹窗的实现代码
Feb 19 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
xml+php动态载入与分页
2006/10/09 PHP
PHP求小于1000的所有水仙花数的代码
2012/01/10 PHP
php实现网页上一页下一页翻页过程详解
2019/06/28 PHP
php命令行模式代码实例详解
2021/02/26 PHP
漂亮的widgets,支持换肤和后期开发新皮肤
2007/04/23 Javascript
IE6下JS动态设置图片src地址问题
2010/01/08 Javascript
使用SyntaxHighlighter实现HTML高亮显示代码的方法
2010/02/04 Javascript
javascript onmouseout 解决办法
2010/07/17 Javascript
Javascript中的isNaN函数使用说明
2011/11/10 Javascript
使用js操作cookie的一点小收获分享
2013/09/03 Javascript
简洁Ajax函数处理(示例代码)
2013/11/15 Javascript
jQuery晃动层特效实现方法
2015/03/09 Javascript
微信小程序实战之运维小项目
2017/01/17 Javascript
js变量值传到php过程详解 将php解析成数据
2019/06/26 Javascript
JS实现纵向轮播图(初级版)
2020/01/18 Javascript
[44:58]2018DOTA2亚洲邀请赛 4.5 淘汰赛 LGD vs Liquid 第二场
2018/04/06 DOTA
Python迭代用法实例教程
2014/09/08 Python
python中文编码与json中文输出问题详解
2018/08/24 Python
python实现BP神经网络回归预测模型
2019/08/09 Python
python ubplot使用方法解析
2020/01/10 Python
keras 自定义loss层+接受输入实例
2020/06/28 Python
泰海淘:泰国king Power王权免税集团旗下跨境海淘综合型电商
2020/07/26 全球购物
C# Debug和Testing相关面试题
2015/10/25 面试题
幼儿园中班新学期寄语
2014/01/18 职场文书
教师专业自荐书范文
2014/02/10 职场文书
报纸媒体创意广告词
2014/03/17 职场文书
乡村卫生服务一体化管理实施方案
2014/03/30 职场文书
学习经验交流会主持词
2014/04/01 职场文书
医疗器械售后服务承诺书
2014/05/21 职场文书
交通事故委托书范本精选
2014/10/04 职场文书
实施意见格式范本
2015/06/05 职场文书
会计岗位工作总结
2015/08/12 职场文书
Golang中interface{}转为数组的操作
2021/04/30 Golang
Python中文纠错的简单实现
2021/07/07 Python
MySQL 数据 data 基本操作
2022/05/04 MySQL
redis protocol通信协议及使用详解
2022/07/15 Redis