python编程开发之textwrap文本样式处理技巧

 更新时间:2015年11月13日 11:24:52   作者:Hongten  
这篇文章主要介绍了python编程开发之textwrap文本样式处理技巧,实例分析了Python中textwrap的常用方法与处理文本样式的相关使用技巧,需要的朋友可以参考下

本文实例讲述了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 装饰器@,对函数进行功能扩展操作示例【开闭原则】

    这篇文章主要介绍了Python 装饰器@,对函数进行功能扩展操作,结合实例形式分析了装饰器的相关使用技巧,以及开闭原则下的函数功能扩展,需要的朋友可以参考下
    2019-10-10
  • 如何用python将文件夹内多个excel表格合并成总表

    如何用python将文件夹内多个excel表格合并成总表

    前几天遇见这么一个问题,手上有很多张表格,这些表格中都只有一个sheet,需要把这些表汇总到一张表,下面这篇文章主要给大家介绍了关于如何用python将文件夹内多个excel表格合并成总表的相关资料,需要的朋友可以参考下
    2023-06-06
  • python实现数字炸弹游戏

    python实现数字炸弹游戏

    这篇文章主要为大家详细介绍了python实现数字炸弹游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • 将本地Python项目打包成docker镜像上传到服务器并在docker中运行

    将本地Python项目打包成docker镜像上传到服务器并在docker中运行

    Docker是一个开源项目,为开发人员和系统管理员提供了一个开放平台,可以将应用程序构建、打包为一个轻量级容器,并在任何地方运行,这篇文章主要给大家介绍了关于将本地Python项目打包成docker镜像上传到服务器并在docker中运行的相关资料,需要的朋友可以参考下
    2023-12-12
  • 浅谈Python的条件判断语句if/else语句

    浅谈Python的条件判断语句if/else语句

    这篇文章主要介绍了Python的条件判断语句if/else语句,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • 利用python进行数据加载

    利用python进行数据加载

    今天给大家带来的是关于Python的相关知识,文章围绕着python数据加载展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • OpenCV半小时掌握基本操作之对象测量

    OpenCV半小时掌握基本操作之对象测量

    这篇文章主要介绍了OpenCV基本操作之对象测量,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • 几款Python编译器比较与推荐(小结)

    几款Python编译器比较与推荐(小结)

    这篇文章主要介绍了几款Python编译器比较与推荐(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Python自动化之UnitTest框架实战记录

    Python自动化之UnitTest框架实战记录

    这篇文章主要给大家介绍了关于Python自动化之UnitTest框架实战的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • pip install urllib2不能安装的解决方法

    pip install urllib2不能安装的解决方法

    今天小编就为大家分享一篇pip install urllib2不能安装的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06

最新评论