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搭建APNS苹果推送通知推送服务的相关模块使用指南
Jun 02 Python
Python的爬虫框架scrapy用21行代码写一个爬虫
Apr 24 Python
Python中执行存储过程及获取存储过程返回值的方法
Oct 07 Python
python统计字母、空格、数字等字符个数的实例
Jun 29 Python
Python人工智能之路 之PyAudio 实现录音 自动化交互实现问答
Aug 13 Python
python3.6中@property装饰器的使用方法示例
Aug 17 Python
PyTorch实现ResNet50、ResNet101和ResNet152示例
Jan 14 Python
Tensorflow累加的实现案例
Feb 05 Python
多个版本的python共存时使用pip的正确做法
Oct 26 Python
python基于opencv实现人脸识别
Jan 04 Python
Python下opencv库的安装过程及问题汇总
Jun 11 Python
Python可视化神器pyecharts绘制水球图
Jul 07 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判断一个字符串是否是回文字符串的方法
2015/03/23 PHP
php fseek函数读取大文件两种方法
2016/10/12 PHP
php下载远程大文件(获取远程文件大小)的实例
2017/06/17 PHP
javascript中全局对象的parseInt()方法使用介绍
2013/12/19 Javascript
jquery.Ajax()方法调用Asp.Net后台的方法解析
2014/02/13 Javascript
JavaScript阻止回车提交表单的方法
2015/12/30 Javascript
js中实现字符串和数组的相互转化详解
2016/01/24 Javascript
Vue.js实战之利用vue-router实现跳转页面
2017/04/01 Javascript
20行JS代码实现网页刮刮乐效果
2017/06/23 Javascript
Three.js利用性能插件stats实现性能监听的方法
2017/09/25 Javascript
JavaScript 五大常见函数
2018/03/23 Javascript
vue中mint-ui的使用方法
2018/04/04 Javascript
Node.js中你不可不精的Stream(流)
2018/06/08 Javascript
vue用Object.defineProperty手写一个简单的双向绑定的示例
2018/07/09 Javascript
基于Vue实现的多条件筛选功能的详解(类似京东和淘宝功能)
2019/05/07 Javascript
vue点击自增和求和的实例代码
2019/11/06 Javascript
[37:23]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#2Secret VS EG第二局
2016/03/04 DOTA
[46:12]完美世界DOTA2联赛循环赛 DM vs Matador BO2第一场 11.04
2020/11/04 DOTA
在Python的Flask框架中使用日期和时间的教程
2015/04/21 Python
Python处理字符串之isspace()方法的使用
2015/05/19 Python
python 读取txt中每行数据,并且保存到excel中的实例
2018/04/29 Python
python实现批量修改图片格式和尺寸
2018/06/07 Python
Python实现提取XML内容并保存到Excel中的方法
2018/09/01 Python
wxPython多个窗口的基本结构
2019/11/19 Python
Python抓包并解析json爬虫的完整实例代码
2020/11/03 Python
python定义具名元组实例操作
2021/02/28 Python
学年自我鉴定范文
2013/10/01 职场文书
兼职学生的自我评价
2013/11/24 职场文书
反四风对照检查材料
2014/09/22 职场文书
2016年高校自主招生自荐信范文
2015/03/24 职场文书
2015年读书月活动总结
2015/03/26 职场文书
2017新年晚会开幕词
2016/03/03 职场文书
Python中tkinter的用户登录管理的实现
2021/04/22 Python
python如何进行基准测试
2021/04/26 Python
每日六道java新手入门面试题,通往自由的道路
2021/06/30 Java/Android
阿里云服务器部署mongodb的详细过程
2021/09/04 MongoDB