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 相关文章推荐
Django1.7+python 2.78+pycharm配置mysql数据库教程
Nov 18 Python
window下eclipse安装python插件教程
Apr 24 Python
Django实现分页功能
Jul 02 Python
python re正则匹配网页中图片url地址的方法
Dec 20 Python
pycharm编写spark程序,导入pyspark包的3中实现方法
Aug 02 Python
python进阶之自定义可迭代的类
Aug 20 Python
python实现两个文件夹的同步
Aug 29 Python
python 画函数曲线示例
Dec 04 Python
使用 Python 读取电子表格中的数据实例详解
Apr 17 Python
python设置中文界面实例方法
Oct 27 Python
pdf论文中python画的图Type 3 fonts字体不兼容的解决方案
Apr 24 Python
Python中super().__init__()测试以及理解
Dec 06 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获取url的函数代码
2011/08/02 PHP
php支持中文字符串分割的函数
2015/05/28 PHP
Yii+upload实现AJAX上传图片的方法
2016/07/13 PHP
CI框架实现优化文件上传及多文件上传的方法
2017/01/04 PHP
Yii框架getter与setter方法功能与用法分析
2019/10/22 PHP
laravel框架select2多选插件初始化默认选中项操作示例
2020/02/18 PHP
【消息提示组件】,兼容IE6/7&&FF2
2007/09/04 Javascript
Jqgrid表格随窗口大小改变而改变的简单实例
2013/12/28 Javascript
Javascript保存网页为图片借助于html2canvas库实现
2014/09/05 Javascript
js闭包实例汇总
2014/11/09 Javascript
Javascript前端UI框架Kit使用指南之Kitjs简介
2014/11/28 Javascript
RequireJS入门一之实现第一个例子
2015/09/30 Javascript
JS函数定义方式的区别介绍
2016/03/22 Javascript
微信小程序 页面跳转和数据传递实例详解
2017/01/19 Javascript
JS利用cookies设置每隔24小时弹出框
2017/04/20 Javascript
使用JS动态显示文本
2017/09/09 Javascript
vue实现微信二次分享以及自定义分享的示例
2019/03/20 Javascript
js+canvas实现转盘效果(两个版本)
2020/09/13 Javascript
[01:33:14]LGD vs VP Supermajor 败者组决赛 BO3 第二场 6.10
2018/07/04 DOTA
Python下使用Psyco模块优化运行速度
2015/04/05 Python
Odoo中如何生成唯一不重复的序列号详解
2018/02/10 Python
Python实现重建二叉树的三种方法详解
2018/06/23 Python
Python图像处理实现两幅图像合成一幅图像的方法【测试可用】
2019/01/04 Python
Python读取xlsx文件的实现方法
2019/07/04 Python
django 快速启动数据库客户端程序的方法示例
2019/08/16 Python
利用OpenCV和Python实现查找图片差异
2019/12/19 Python
jupyter notebook 多行输出实例
2020/04/09 Python
理解Django 中Call Stack机制的小Demo
2020/09/01 Python
Python监听剪切板实现方法代码实例
2020/11/11 Python
CSS3 伪类选择器 nth-child()说明
2010/07/10 HTML / CSS
爱尔兰领先的在线体育用品零售商:theGAAstore
2018/04/16 全球购物
蒙蒂塞罗商店:Monticello Shop
2018/11/25 全球购物
幼儿园长自我鉴定
2013/10/17 职场文书
白血病募捐倡议书
2014/05/14 职场文书
对照四风自我剖析材料
2014/10/07 职场文书
2015年学校禁毒工作总结
2015/05/27 职场文书