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实现决策树ID3算法的示例代码
May 30 Python
Python3.6+Django2.0以上 xadmin站点的配置和使用教程图解
Jun 04 Python
在python Numpy中求向量和矩阵的范数实例
Aug 26 Python
python基于opencv检测程序运行效率
Dec 28 Python
如何通过Django使用本地css/js文件
Jan 20 Python
Python GUI库PyQt5图形和特效样式QSS介绍
Feb 25 Python
Tensorflow实现将标签变为one-hot形式
May 22 Python
python实现粒子群算法
Oct 15 Python
Python基于opencv的简单图像轮廓形状识别(全网最简单最少代码)
Jan 28 Python
python实现学生通讯录管理系统
Feb 25 Python
python如何发送带有附件、正文为HTML的邮件
Feb 27 Python
Python 如何将integer转化为罗马数(3999以内)
Jun 05 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
微信公众平台开发教程⑤ 微信扫码支付模式介绍
2019/04/10 PHP
redis+php实现微博(三)微博列表功能详解
2019/09/23 PHP
图片按比例缩放函数
2006/06/26 Javascript
模仿jQuery each函数的链式调用
2009/07/22 Javascript
jQuery之浮动窗口实现代码(两种方法)
2010/09/08 Javascript
IFrame跨域高度自适应实现代码
2012/08/16 Javascript
js点击更换背景颜色或图片的实例代码
2013/06/25 Javascript
各种页面定时跳转(倒计时跳转)代码总结
2013/10/24 Javascript
写JQuery插件的基本知识
2013/11/25 Javascript
JavaScript lastIndexOf方法入门实例(计算指定字符在字符串中最后一次出现的位置)
2014/10/17 Javascript
使用javascript实现简单的选项卡切换
2015/01/09 Javascript
jQuery插件Elastislide实现响应式的焦点图无缝滚动切换特效
2015/04/12 Javascript
js实现漂浮回顶部按钮实例
2015/05/06 Javascript
记React connect的几种写法(小结)
2018/09/18 Javascript
jquery实现动态添加附件功能
2018/10/23 jQuery
详解vue配置后台接口方式
2019/03/29 Javascript
Vue的属性、方法、生命周期实例代码详解
2019/09/17 Javascript
[48:26]VGJ.S vs infamous Supermajor 败者组 BO3 第二场 6.4
2018/06/05 DOTA
http请求 request失败自动重新尝试代码示例
2018/01/25 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
2018/04/02 Python
Python中Subprocess的不同函数解析
2019/12/10 Python
python标准库sys和OS的函数使用方法与实例详解
2020/02/12 Python
python实现猜单词游戏
2020/05/22 Python
Python几种常见算法汇总
2020/06/02 Python
利用Vscode进行Python开发环境配置的步骤
2020/06/22 Python
sklearn的predict_proba使用说明
2020/06/28 Python
Reebok俄罗斯官方网上商店:购买锐步运动服装和鞋子
2016/09/26 全球购物
屈臣氏官方旗舰店:亚洲享负盛名的保健及美妆零售商
2019/03/15 全球购物
耐克亚太地区:Nike APAC
2019/12/07 全球购物
统计员岗位职责
2013/11/14 职场文书
如何写好升职自荐信
2014/01/06 职场文书
揭牌仪式主持词
2014/03/19 职场文书
2015年大学团支部工作总结
2015/05/13 职场文书
2015暑期社会实践通讯稿
2015/07/18 职场文书
幼儿园毕业致辞
2015/07/29 职场文书
Android超详细讲解组件ScrollView的使用
2022/03/31 Java/Android