textwrap模块1-fill()

来源:互联网 发布:柯桥司法拍卖淘宝网 编辑:程序博客网 时间:2024/05/14 15:42

fill()函数取文本作为输入,生成格式化的文本作为输出。

一、生成一个示例数据模块(textwrap_example.py)

sample_text = '''
    The textwrap module can be used to format text for output in
    situations where pretty-printing is desired. It offers
    programmatic functionality similar to the paragraph wrapping
    or filling features found in many text editors.
    '''

二、利用fill函数进行格式化输出

import textwrap

from textwrap_example import sample_text

print 'No dedent:\n'

print textwrap.fill(sample_text,width=50)

结果:

No dedent:

     The textwrap module can be used to format
text for output in     situations where pretty-
printing is desired. It offers     programmatic
functionality similar to the paragraph wrapping
or filling features found in many text editors.


如果width的值为60,

结果:

No dedent:

     The textwrap module can be used to format text for
output in     situations where pretty-printing is desired.
It offers     programmatic functionality similar to the
paragraph wrapping     or filling features found in many
text editors.


原创粉丝点击