python編程開發之textwrap文本樣式處理技巧

來源:互聯網
上載者:User
本文執行個體講述了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 win32Type "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 win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>>   The textwrap module provides twoconvenience functions, wrap() andfill(), as well as 1   TextWrapper,the class that does all the work, andtwo utility functions, dedent() andindent(). If 2   you're just wrappingor filling one or two text strings, theconvenience functions should be good 3enough; otherwise, you should use aninstance 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 win32Type "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程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.