首页 > 其他 > 详细

textwrap——文本包裹和填充模块解析

时间:2015-10-09 20:01:21      阅读:149      评论:0      收藏:0      [点我收藏+]

        textwrap模块提供了两个函数wrap()和fill(),以及TextWrapper类,以及另外一个工具函数dedent()。

        wrap()以及fill()都可以用来格式化一大段文本,将指定文本限制在一定的屏幕宽度。例如
  1.  1 >>> import textwrap
     2 >>> doc = """The wrap() method is just like fill() except that it returns
     3 ... a list of strings instead of one big string with newlines to separate
     4 ... the wrapped lines."""
     5 ...
     6 >>> print textwrap.fill(doc, width=40)
     7 The wrap() method is just like fill()
     8 except that it returns a list of strings
     9 instead of one big string with newlines
    10 to separate the wrapped lines.
        wrap()和fill()的区别是,wrap()返回的是个列表,而fill()返回一个字符串。
        dedent()用来清除空白字符。例如
  1. 1 def test():
    2     # end first line with \ to avoid the empty line!
    3     s = ‘‘‘4     hello
    5       world
    6     ‘‘‘
    7     print repr(s)          # prints ‘    hello\n      world\n    ‘
    8     print repr(dedent(s))  # prints ‘hello\n  world\n‘
        此外也可以使用TextWrapper类构造对象,例如:
  1. 1 wrapper =TextWrapper(initial_indent="* ")

 

textwrap——文本包裹和填充模块解析

原文:http://www.cnblogs.com/fireflow/p/4864859.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!