当前位置:天才代写 > tutorial > Python教程 > python模块进修- textwrap 文本包装和填充

python模块进修- textwrap 文本包装和填充

2017-11-03 08:00 星期五 所属: Python教程 浏览:443

python模块进修- textwrap 文本包装和填充

代码实例:

sample_text = '''

   The textwrap module can beused to format text for output in

   situations wherepretty-printing is desired.  It offers

   programmatic functionalitysimilar to the paragraph wrapping

   or filling features found inmany text editors.

'''

段落填充:

import textwrap
from textwrap_exampleimport sample_text
  
print 'Nodedent:\n'
printtextwrap.fill(sample_text, width=50)

执行功效:

# pythontextwrap_fill.py

No dedent:

    The textwrap module can be used to format

text for outputin     situations where pretty-

printing is desired.  It offers    programmatic

functionalitysimilar to the paragraph wrapping

or fillingfeatures found in many text editors.

功效为左对齐,第一行有缩进。行中的空格继承保存。

移除缩进:

import textwrap
fromtextwrap_example import sample_text
  
dedented_text = textwrap.dedent(sample_text)
print 'Dedented:'
printdedented_text

执行功效:

# pythontextwrap_dedent.py

Dedented:

The textwrapmodule can be used to format text for output in

situations wherepretty-printing is desired.  It offers

programmaticfunctionality similar to the paragraph wrapping

or fillingfeatures found in many text editors.

这样第一行就不会缩进。

团结移除缩进和填充:

import textwrap
fromtextwrap_example import sample_text
  
dedented_text =textwrap.dedent(sample_text).strip()
for width in [ 45,70 ]:
       print '%d Columns:\n' % width
       print textwrap.fill(dedented_text,width=width)
       print

执行功效:

# pythontextwrap_fill_width.py

45 Columns:

The textwrapmodule can be used to format

text for output insituations where pretty-

printing isdesired.  It offers programmatic

functionalitysimilar to the paragraph

wrapping orfilling features found in many

text editors.

70 Columns:

The textwrapmodule can be used to format text for output in

situations wherepretty-printing is desired.  It offersprogrammatic

functionality similarto the paragraph wrapping or filling features

found in many texteditors.

悬挂缩进:悬挂缩进第一行的缩进小于其他行的缩进。

import textwrap
fromtextwrap_example import sample_text
  
dedented_text =textwrap.dedent(sample_text).strip()
printtextwrap.fill(dedented_text,
                    initial_indent='',
                    subsequent_indent=' ' * 4,
                    width=50,
                    )
       执行功效:
# pythontextwrap_hanging_indent.py
The textwrapmodule can be used to format text for
    output in situations where pretty-printingis
    desired. It offers programmatic functionality
    similar to the paragraph wrapping orfilling
    features found in many text editors.

个中的’’*4还可以利用其他字符取代。

             TextWrap提供函数wrap()和fill(), 以及TextWrapper类,东西函数dedent(). 凡是包装可能填充一两个字符串利用wrap()和fill()。其他环境利用TextWrapper更高效。

textwrap.wrap(text[,width[, …]])

包装单个段落(text为输入,系字符串),每行最长宽度width。返回输出行的列表,最后行无换行符。Width默认70。

textwrap.fill(text[,width[, …]])

包装单段文字,并返回包括包裹段落的字符串。实际上是"\n".join(wrap(text,…))的缩写。wrap() and fill()建设TextWrapper实例,并挪用一个要领。这些实例不被重用,所以包装/填充许多文本字符串要结构本身的TextWrapper工具更有效。TextWrapper.break_long_words配置是否拆长单词。

textwrap.dedent(text)

反缩进去除每行行首的空缺。这利便显示三引号中的内容而不修改其源代码中的缩进。

 

    关键字:

天才代写-代写联系方式