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装饰器入门学习教程(九步学习)
Jan 28 Python
简述Python中的进程、线程、协程
Mar 18 Python
python先序遍历二叉树问题
Nov 10 Python
python基于物品协同过滤算法实现代码
May 31 Python
浅谈tensorflow中几个随机函数的用法
Jul 27 Python
python itchat实现调用微信接口的第三方模块方法
Jun 11 Python
python快速编写单行注释多行注释的方法
Jul 31 Python
Python random模块制作简易的四位数验证码
Feb 01 Python
Win 10下Anaconda虚拟环境的教程
May 18 Python
python3实现简单飞机大战
Nov 29 Python
python自动生成证件号的方法示例
Jan 14 Python
Python中lru_cache的使用和实现详解
Jan 25 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
全国FM电台频率大全 - 20 广西省
2020/03/11 无线电
PHP新手上路(四)
2006/10/09 PHP
PHP持久连接mysql_pconnect()函数使用介绍
2012/02/05 PHP
PHP文件锁函数flock()详细介绍
2014/11/18 PHP
php缓冲输出实例分析
2015/01/05 PHP
分享常见的几种页面静态化的方法
2015/01/08 PHP
php生成毫秒时间戳的实例讲解
2017/09/22 PHP
javascript的函数
2007/01/31 Javascript
js导航栏单击事件背景变换示例代码
2014/01/13 Javascript
jquery列表拖动排列(由项目提取相当好用)
2014/06/17 Javascript
JS实现左右无缝轮播图代码
2016/05/01 Javascript
jQuery中$.ajax()方法参数解析
2016/10/22 Javascript
Es6 Generator函数详细解析
2018/02/24 Javascript
vuejs使用axios异步访问时用get和post的实例讲解
2018/08/09 Javascript
微信小程序实现签到功能
2018/10/31 Javascript
JS实现容器模块左右拖动效果
2020/01/14 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
微信小程序学习总结(五)常见问题实例小结
2020/06/04 Javascript
微信小程序实现电影App导航和轮播
2020/11/30 Javascript
[03:18]【TI9纪实】社区大触GL与木木
2019/08/25 DOTA
python getopt 参数处理小示例
2009/06/09 Python
Python数据结构之哈夫曼树定义与使用方法示例
2018/04/22 Python
python数据批量写入ScrolledText的优化方法
2018/10/11 Python
python 输入一个数n,求n个数求乘或求和的实例
2018/11/13 Python
详解pandas使用drop_duplicates去除DataFrame重复项参数
2019/08/01 Python
OpenCV图片漫画效果的实现示例
2020/08/18 Python
StubHub希腊:购买体育赛事、音乐会和剧院门票
2019/08/03 全球购物
介绍一下代理模式(Proxy)
2014/10/17 面试题
会计主管岗位职责范文
2013/11/08 职场文书
业务代表的岗位职责
2013/11/16 职场文书
房地产开发项目建议书
2014/05/16 职场文书
2015年万圣节活动总结
2015/03/24 职场文书
工程款申请报告
2015/05/15 职场文书
2015小学师德工作总结
2015/07/21 职场文书
2016党员干部反腐倡廉心得体会
2016/01/13 职场文书
Java中常用解析工具jackson及fastjson的使用
2021/06/28 Java/Android