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回调函数用法实例分析
May 09 Python
python类装饰器用法实例
Jun 04 Python
解决Python的str强转int时遇到的问题
Apr 09 Python
解决python xlrd无法读取excel文件的问题
Dec 25 Python
十分钟搞定pandas(入门教程)
Jun 21 Python
python中单下划线(_)和双下划线(__)的特殊用法
Aug 29 Python
Python(PyS60)实现简单语音整点报时
Nov 18 Python
探秘TensorFlow 和 NumPy 的 Broadcasting 机制
Mar 13 Python
使用Python获取爱奇艺电视剧弹幕数据的示例代码
Jan 12 Python
OpenCV-Python直方图均衡化实现图像去雾
Jun 07 Python
解决pycharm安装scrapy DLL load failed:找不到指定的程序的问题
Jun 08 Python
Django路由层如何获取正确的url
Jul 15 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编码规范-php coding standard
2007/03/16 PHP
理解PHP中的stdClass类
2014/04/18 PHP
php制作简单模版引擎
2016/04/07 PHP
硬盘浏览程序,保存成网页格式便可使用
2006/12/03 Javascript
javascript真的不难-回顾一下基础知识
2013/01/15 Javascript
js的延迟执行问题分析
2014/06/23 Javascript
兼容IE、firefox以及chrome的js获取时间(getFullYear)
2014/07/04 Javascript
JavaScript中的数学运算介绍
2014/12/29 Javascript
基于jQuery实现Div窗口震动特效代码-代码简单
2015/08/28 Javascript
多种js图片预加载实现方式分享
2016/02/19 Javascript
基于JavaScript实现百度搜索框效果
2020/06/28 Javascript
jQuery简单实现对数组去重及排序操作实例
2017/10/31 jQuery
微信小程序解除10个请求并发限制
2018/12/18 Javascript
微信小程序引入模块中wxml、wxss、js的方法示例
2019/08/09 Javascript
layui table设置某一行的字体颜色方法
2019/09/05 Javascript
详解微信小程序「渲染层网络层错误」的解决方法
2021/01/06 Javascript
[01:08:24]DOTA2-DPC中国联赛 正赛 RNG vs Phoenix BO3 第一场 2月5日
2021/03/11 DOTA
利用打码兔和超人打码自封装的打码类分享
2014/03/16 Python
python实现linux服务器批量修改密码并生成execl
2014/04/22 Python
Django发送html邮件的方法
2015/05/26 Python
详解Python的Flask框架中生成SECRET_KEY密钥的方法
2016/06/07 Python
Python Flask-web表单使用详解
2017/11/18 Python
django 在原有表格添加或删除字段的实例
2018/05/27 Python
Python实现的绘制三维双螺旋线图形功能示例
2018/06/23 Python
python 整数越界问题详解
2019/06/27 Python
pytorch随机采样操作SubsetRandomSampler()
2020/07/07 Python
利用Python将多张图片合成视频的实现
2020/11/23 Python
美国第二大团购网站:LivingSocial
2016/07/24 全球购物
美国网上书店:Barnes & Noble
2018/08/15 全球购物
Diptyque英国官方网站:源自法国的知名香氛品牌
2019/08/28 全球购物
校本教研活动总结
2014/07/01 职场文书
电子商务专业求职信
2014/07/10 职场文书
五一晚会主持词
2015/07/01 职场文书
辞职离别感言
2015/08/04 职场文书
解决MultipartFile.transferTo(dest) 报FileNotFoundExcep的问题
2021/07/01 Java/Android
浅谈redis的过期时间设置和过期删除机制
2022/03/18 MySQL