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的Flask框架中配置多个子域名的方法讲解
Jun 07 Python
Python在线运行代码助手
Jul 15 Python
python tensorflow基于cnn实现手写数字识别
Jan 01 Python
详解Numpy中的广播原则/机制
Sep 20 Python
在Python中增加和插入元素的示例
Nov 01 Python
python 多线程串行和并行的实例
Feb 22 Python
Python使用os.listdir()和os.walk()获取文件路径与文件下所有目录的方法
Apr 01 Python
详解Python Qt的窗体开发的基本操作
Jul 14 Python
Python如何调用JS文件中的函数
Aug 16 Python
python ffmpeg任意提取视频帧的方法
Feb 21 Python
keras实现调用自己训练的模型,并去掉全连接层
Jun 09 Python
Python 防止死锁的方法
Jul 29 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多用户读写文件冲突的解决办法
2013/11/06 PHP
PHP rsa加密解密使用方法
2015/04/27 PHP
yii实现model添加默认值的方法(2种方法)
2016/01/06 PHP
PHP实现QQ登录实例代码
2016/01/14 PHP
Laravel框架下载,安装及路由操作图文详解
2019/12/04 PHP
tagName的使用,留一笔
2006/06/26 Javascript
比较简单的异步加载JS文件的代码
2009/07/18 Javascript
很好用的js日历算法详细代码
2013/03/07 Javascript
Jquery中使用setInterval和setTimeout的方法
2013/04/08 Javascript
让js弹出窗口居前显示的实现方法
2013/07/10 Javascript
javascript浏览器兼容教程之事件处理
2014/06/09 Javascript
简介JavaScript中用于处理正切的Math.tan()方法
2015/06/15 Javascript
JS实现类似51job上的地区选择效果示例
2016/11/17 Javascript
从零学习node.js之express入门(六)
2017/02/25 Javascript
详解Vue.js 2.0 如何使用axios
2017/04/21 Javascript
Node.js实现mysql连接池使用事务自动回收连接的方法示例
2018/02/03 Javascript
Vue表单及表单绑定方法
2018/09/04 Javascript
vue flex 布局实现div均分自动换行的示例代码
2020/08/05 Javascript
原生JavaScript实现幻灯片效果
2021/02/19 Javascript
python检查URL是否正常访问的小技巧
2017/02/25 Python
在Python中append以及extend返回None的例子
2019/07/20 Python
python利用dlib获取人脸的68个landmark
2019/11/27 Python
Python中flatten( ),matrix.A用法说明
2020/07/05 Python
Python如何操作docker redis过程解析
2020/08/10 Python
python中函数返回多个结果的实例方法
2020/12/16 Python
CSS Grid布局教程之网格单元格布局
2014/12/30 HTML / CSS
CSS3实现多重边框的方法总结
2016/05/31 HTML / CSS
html5唤起app的方法
2017/11/30 HTML / CSS
瑜伽服装品牌:露露柠檬(lululemon athletica)
2017/06/04 全球购物
Python面试题:Python里面如何生成随机数
2015/03/12 面试题
优秀员工推荐信
2014/05/10 职场文书
餐饮食品安全责任书
2015/01/29 职场文书
《悲惨世界》:比天空更广阔的是人的心灵
2020/01/16 职场文书
MySQL表字段时间设置默认值
2021/05/13 MySQL
古见同学有交流障碍症 第二季宣传CM公开播出
2022/04/11 日漫
Spring中bean集合注入的方法详解
2022/07/07 Java/Android