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 SQLite3数据库日期与时间常见函数用法分析
Aug 14 Python
Django中使用celery完成异步任务的示例代码
Jan 23 Python
python调用Matplotlib绘制分布点并且添加标签
May 31 Python
Python 访问限制 private public的详细介绍
Oct 16 Python
对python操作kafka写入json数据的简单demo分享
Dec 27 Python
Django 对象关系映射(ORM)源码详解
Aug 06 Python
Pytorch的mean和std调查实例
Jan 02 Python
Python实现图像的垂直投影示例
Jan 17 Python
Pytorch高阶OP操作where,gather原理
Apr 30 Python
python使用for...else跳出双层嵌套循环的方法实例
May 17 Python
Python并发请求下限制QPS(每秒查询率)的实现代码
Jun 05 Python
基于Python pyecharts实现多种图例代码解析
Aug 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
虹吸壶是谁发明的?煮出来的咖啡好喝吗
2021/03/04 冲泡冲煮
php IP及IP段进行访问限制的代码
2008/12/17 PHP
探讨PHP调用时间格式的参数详解
2013/06/06 PHP
php+jQuery+Ajax简单实现页面异步刷新
2016/08/08 PHP
PHP中ID设置自增后不连续的原因分析及解决办法
2016/08/21 PHP
PHP在linux上执行外部命令的方法
2017/02/06 PHP
为何说PHP引用是个坑,要慎用
2018/04/02 PHP
HTML长文本截取含有HTML代码同样适用的两种方法
2013/07/31 Javascript
js 剪切板应用clipboardData详细解析
2013/12/17 Javascript
三种检测iPhone/iPad设备方向的方法
2014/04/23 Javascript
JavaScript及jquey实现多个数组的合并操作
2014/09/06 Javascript
jQuery中用dom操作替代正则表达式
2014/12/29 Javascript
js获取数组的最后一个元素
2015/04/14 Javascript
JS动态创建元素的两种方法
2016/04/20 Javascript
js实现网页定位导航功能
2017/03/07 Javascript
JavaScript设置名字输入不合法的实现方法
2017/05/23 Javascript
深入掌握 react的 setState的工作机制
2017/09/27 Javascript
利用百度地图API获取当前位置信息的实例
2017/11/06 Javascript
Koa2 之文件上传下载的示例代码
2018/03/29 Javascript
axios 处理 302 状态码的解决方法
2018/04/10 Javascript
原生JS封装_new函数实现new关键字的功能
2018/08/12 Javascript
Koa 使用小技巧(小结)
2018/10/22 Javascript
node.js中 redis 的安装和基本操作示例
2020/02/10 Javascript
解决Ant Design Modal内嵌Form表单initialValue值不动态更新问题
2020/10/29 Javascript
Python使用urllib2模块抓取HTML页面资源的实例分享
2016/05/03 Python
Python使用re模块正则提取字符串中括号内的内容示例
2018/06/01 Python
python 画二维、三维点之间的线段实现方法
2019/07/07 Python
python实现最大优先队列
2019/08/29 Python
Python 如何实现数据库表结构同步
2020/09/29 Python
美国知名的网上鞋类及相关服装零售商:Shoes.com
2017/05/06 全球购物
应届生服务员求职信
2013/10/31 职场文书
艺术设计专业个人求职信范文
2013/12/11 职场文书
土木工程专业推荐信
2014/02/19 职场文书
群众路线党员个人整改措施
2014/10/27 职场文书
自主招生自荐信怎么写
2015/03/24 职场文书
jQuery实现影院选座订座效果
2021/04/13 jQuery