python编程开发之textwrap文本样式处理技巧
本文实例讲述了python编程开发之textwrap文本样式处理技巧。分享给大家供大家参考,具体如下:
在看python的API的时候,发现python的textwrap在处理字符串样式的时候功能强大
在这里我做了一个demo:
textwrap提供了一些方法:
wrap(text,width=70,**kwargs):这个函数可以把一个字符串拆分成一个序列
fromtextwrapimport* #使用textwrap中的wrap()方法 deftest_wrap(): test_str='''\ Thetextwrapmoduleprovidestwoconveniencefunctions,wrap()andfill(),aswellas1 TextWrapper,theclassthatdoesallthework,andtwoutilityfunctions,dedent()andindent().If2 you'rejustwrappingorfillingoneortwotextstrings,theconveniencefunctionsshouldbegood3 enough;otherwise,youshoulduseaninstanceofTextWrapperforefficiency.4 ''' print(wrap(test_str,20)) defmain(): test_wrap() if__name__=='__main__': main()
输出效果:
Python3.3.2(v3.3.2:d047928ae3f6,May162013,00:03:43)[MSCv.160032bit(Intel)]onwin32 Type"copyright","credits"or"license()"formoreinformation. >>>================================RESTART================================ >>> ['Thetextwrap','moduleprovidestwo','convenience','functions,wrap()','andfill(),aswell','as1','TextWrapper,the','classthatdoesall','thework,andtwo','utilityfunctions,','dedent()and','indent().If2','you'rejustwrapping','orfillingoneor','twotextstrings,','theconvenience','functionsshouldbe','good3enough;','otherwise,you','shouldusean','instanceof','TextWrapperfor','efficiency.4'] >>>
我们会发现,wrap()函数,把字符串拆分成了一个序列,在这个序列中,每个元素的长度是一样的。
fill(text,width=70,**kwargs):该方法可以根据指定的长度,进行拆分字符串,然后逐行显示
fromtextwrapimport* #fill()方法 deftest_wrap(): test_str='''\ Thetextwrapmoduleprovidestwoconveniencefunctions,wrap()andfill(),aswellas1 TextWrapper,theclassthatdoesallthework,andtwoutilityfunctions,dedent()andindent().If2 you'rejustwrappingorfillingoneortwotextstrings,theconveniencefunctionsshouldbegood3 enough;otherwise,youshoulduseaninstanceofTextWrapperforefficiency.4 ''' print(fill(test_str,40)) defmain(): test_wrap() if__name__=='__main__': main()
运行效果:
Python3.3.2(v3.3.2:d047928ae3f6,May162013,00:03:43)[MSCv.160032bit(Intel)]onwin32 Type"copyright","credits"or"license()"formoreinformation. >>>================================RESTART================================ >>> Thetextwrapmoduleprovidestwo conveniencefunctions,wrap()and fill(),aswellas1TextWrapper, theclassthatdoesallthework,and twoutilityfunctions,dedent()and indent().If2you'rejustwrapping orfillingoneortwotextstrings,the conveniencefunctionsshouldbegood3 enough;otherwise,youshouldusean instanceofTextWrapperforefficiency. >>>
dedent()方法->文本进行不缩进显示,相应的indent()方法->进行缩进显示
fromtextwrapimport* #dedent()方法 deftest_wrap(): test_str='''\ Thetextwrapmoduleprovidestwoconvenience functions,wrap()andfill(),aswellas1 TextWrapper,theclassthatdoesallthework, andtwoutilityfunctions,dedent()andindent().If2 you'rejustwrappingorfillingoneortwotextstrings, theconveniencefunctionsshouldbegood3 enough;otherwise,youshoulduseaninstance ofTextWrapperforefficiency.4 ''' print(repr(dedent(test_str))) defmain(): test_wrap() if__name__=='__main__': main()
运行效果:
Python3.3.2(v3.3.2:d047928ae3f6,May162013,00:03:43)[MSCv.160032bit(Intel)]onwin32 Type"copyright","credits"or"license()"formoreinformation. >>>================================RESTART================================ >>> 'Thetextwrapmoduleprovidestwoconvenience\nfunctions,wrap()andfill(),aswellas1\nTextWrapper,theclassthatdoesallthework,\nandtwoutilityfunctions,dedent()andindent().If2\nyou'rejustwrappingorfillingoneortwotextstrings,\ntheconveniencefunctionsshouldbegood3\nenough;otherwise,youshoulduseaninstance\nofTextWrapperforefficiency.4\n' >>>
希望本文所述对大家Python程序设计有所帮助。