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中的Numpy 面向数组编程常见操作

    Python中的Numpy 面向数组编程常见操作

    这篇文章主要介绍了Python中的Numpy 面向数组编程常见操作,使用Numpy数组可以使你利用简单的数组表达式完成多项数据操作任务,而不需要编写大量的循环,这个极大的帮助了我们高效的解决问题
    2022-07-07
  • 2019 Python最新面试题及答案16道题

    2019 Python最新面试题及答案16道题

    这篇文章主要介绍了2019 Python最新面试题及答案16道题 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • 从零学Python之hello world

    从零学Python之hello world

    从今天开始讲陆续发布一系列python基础教程,让新手更快更好的入门。
    2014-05-05
  • Python创建一个元素都为0的列表实例

    Python创建一个元素都为0的列表实例

    今天小编就为大家分享一篇Python创建一个元素都为0的列表实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • numpy自动生成数组详解

    numpy自动生成数组详解

    这篇文章主要介绍了numpy自动生成数组详解,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)

    pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)

    这篇文章主要介绍了pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Python深入学习之闭包

    Python深入学习之闭包

    这篇文章主要介绍了Python深入学习之闭包,闭包(closure)是函数式编程的重要的语法结构,Python也支持这一特性,本文就这一特性做了讲解,需要的朋友可以参考下
    2014-08-08
  • python导包的几种方法(自定义包的生成以及导入详解)

    python导包的几种方法(自定义包的生成以及导入详解)

    这篇文章主要介绍了python导包的几种方法(自定义包的生成以及导入详解),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • python 实现对文件夹内的文件排序编号

    python 实现对文件夹内的文件排序编号

    下面小编就为大家分享一篇python 实现对文件夹内的文件排序编号,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • python实现切割url得到域名、协议、主机名等各个字段的例子

    python实现切割url得到域名、协议、主机名等各个字段的例子

    今天小编就为大家分享一篇python实现切割url得到域名、协议、主机名等各个字段的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07

最新评论