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代码制作configure文件示例
Jul 28 Python
Python中处理时间的几种方法小结
Apr 09 Python
python插入排序算法实例分析
Jul 03 Python
Python学习教程之常用的内置函数大全
Jul 14 Python
Django渲染Markdown文章目录的方法示例
Jan 02 Python
使用OpenCV实现仿射变换—缩放功能
Aug 29 Python
Python 矩阵转置的几种方法小结
Dec 02 Python
Python编译成.so文件进行加密后调用的实现
Dec 23 Python
Python程序控制语句用法实例分析
Jan 14 Python
Django 解决model 反向引用中的related_name问题
May 19 Python
Python导入父文件夹中模块并读取当前文件夹内的资源
Nov 19 Python
python中time、datetime模块的使用
Dec 14 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
在Windows系统上安装PHP运行环境文字教程
2010/07/19 PHP
分享一个Laravel好用的Cache宏
2015/03/02 PHP
php+ajax实现商品对比功能示例
2019/04/13 PHP
Thinkphp集成抖音SDK的实现方法
2020/04/28 PHP
不要在cookie中使用特殊字符的原因分析
2010/07/13 Javascript
jquery(live)中File input的change方法只起一次作用的解决办法
2011/10/21 Javascript
网站内容禁止复制和粘贴、另存为的js代码
2014/02/26 Javascript
Jquery实现的角色左右选择特效
2014/05/21 Javascript
使用jquery+CSS实现控制打印样式
2014/12/31 Javascript
15款jQuery分布引导插件分享
2015/02/04 Javascript
基于jquery实现的仿优酷图片轮播特效代码
2016/01/13 Javascript
JavaScript Math 对象常用方法总结
2016/04/28 Javascript
jQuery的层级查找方式分析
2016/06/16 Javascript
完全深入学习Bootstrap表单
2016/11/28 Javascript
AngularJS实现表单验证功能
2017/01/09 Javascript
jQuery插件HighCharts绘制2D柱状图、折线图和饼图的组合图效果示例【附demo源码下载】
2017/03/09 Javascript
vue-devtools的安装步骤
2018/04/23 Javascript
swiper.js插件实现pc端文本上下滑动功能示例
2018/12/03 Javascript
微信小程序文章详情页跳转案例详解
2019/07/09 Javascript
python抓取网页中的图片示例
2014/02/28 Python
Python列表计数及插入实例
2014/12/17 Python
python实现将pvr格式转换成pvr.ccz的方法
2015/04/28 Python
python解决网站的反爬虫策略总结
2016/10/26 Python
python顺序的读取文件夹下名称有序的文件方法
2018/07/11 Python
python3中pip3安装出错,找不到SSL的解决方式
2019/12/12 Python
django inspectdb 操作已有数据库数据的使用步骤
2021/02/07 Python
python中Pexpect的工作流程实例讲解
2021/03/02 Python
app内嵌H5 webview 本地缓存问题的解决
2020/10/19 HTML / CSS
软件测试企业面试试卷
2016/07/13 面试题
平安校园建设方案
2014/05/02 职场文书
2015年学校教科室工作总结
2015/07/20 职场文书
施工安全责任协议书
2016/03/23 职场文书
Mysql中存储引擎的区别及比较
2021/06/04 MySQL
浅谈Python数学建模之线性规划
2021/06/23 Python
业余无线电通联Q语
2022/02/18 无线电
使用HttpSessionListener监听器实战
2022/03/17 Java/Android