python模組:textwrap 文本封裝和填充的代碼執行個體

來源:互聯網
上載者:User

代碼執行個體:

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

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

執行結果:

# 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)

反縮排去除每行行首的空白。這方便顯示三引號中的內容而不修改其原始碼中的縮排。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.