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 多线程应用介绍
Dec 19 Python
Python数据分析之真实IP请求Pandas详解
Nov 18 Python
Pycharm学习教程(5) Python快捷键相关设置
May 03 Python
Php多进程实现代码
May 07 Python
浅析Python函数式编程
Oct 06 Python
Ubuntu下Python2与Python3的共存问题
Oct 31 Python
python提取具有某种特定字符串的行数据方法
Dec 11 Python
python列表每个元素同增同减和列表元素去空格的实例
Jul 20 Python
关于tf.matmul() 和tf.multiply() 的区别说明
Jun 18 Python
Python实现LR1文法的完整实例代码
Oct 25 Python
Python GUI库Tkiner使用方法代码示例
Nov 27 Python
给numpy.array增加维度的超简单方法
Jun 02 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
2006/12/14 PHP
php遍历目录方法小结
2015/03/10 PHP
PHP编写daemon process详解及实例代码
2016/09/30 PHP
PHP中Laravel 关联查询返回错误id的解决方法
2017/04/01 PHP
PHP创建XML接口示例
2019/07/04 PHP
ECMAScript 创建自己的js类库
2012/11/22 Javascript
jQuery实现等比例缩放大图片让大图片自适应页面布局
2013/10/16 Javascript
JQuery核心函数是什么及使用方法介绍
2016/05/03 Javascript
仿百度换肤功能的简单实例代码
2016/07/11 Javascript
Vue2.0子同级组件之间数据交互方法
2018/02/28 Javascript
vue-router 实现导航守卫(路由卫士)的实例代码
2018/09/02 Javascript
详解如何使用微信小程序云函数发送短信验证码
2019/03/13 Javascript
微信内置开发 iOS修改键盘换行为搜索的解决方案
2019/11/06 Javascript
Vue混入mixins滚动触底的方法
2019/11/22 Javascript
vue中用 async/await 来处理异步操作
2020/07/18 Javascript
深入理解 ES6中的 Reflect用法
2020/07/18 Javascript
详解vue中v-model和v-bind绑定数据的异同
2020/08/10 Javascript
[52:08]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#2Fnatic VS OG第一局
2016/03/05 DOTA
用Python写一个无界面的2048小游戏
2016/05/24 Python
基于使用paramiko执行远程linux主机命令(详解)
2017/10/16 Python
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
2018/06/04 Python
python爬取基于m3u8协议的ts文件并合并
2019/04/26 Python
Python Flask框架模板操作实例分析
2019/05/03 Python
Pandas之DataFrame对象的列和索引之间的转化
2019/06/25 Python
Python Web程序搭建简单的Web服务器
2019/07/31 Python
详解Python图像处理库Pillow常用使用方法
2019/09/02 Python
Django连接数据库并实现读写分离过程解析
2019/11/13 Python
Pycharm常用快捷键总结及配置方法
2020/11/14 Python
使用CSS3来制作消息提醒框
2015/07/12 HTML / CSS
韩国著名的在线综合购物网站:Akmall
2016/08/07 全球购物
上海期货面试题
2014/01/31 面试题
文体活动实施方案
2014/03/27 职场文书
人事任命书格式
2014/06/05 职场文书
收银员岗位职责
2015/02/03 职场文书
幼儿园开学温馨提示
2015/07/15 职场文书
2016大学生入党积极分子心得体会
2016/01/06 职场文书