This paper describes the TextWrap Text style processing techniques of Python programming development. Share to everyone for your reference, as follows:
When we look at the Python API, we find that Python's textwrap is powerful when it comes to handling string styles.
Here I made a demo:
TextWrap provides a number of methods:
Wrap (text, width = **kwargs): This function can split a string into a sequence
From textwrap Import * #使用textwrap中的wrap () method Def test_wrap (): test_str = "\ The textwrap module provides the conv Enience functions, wrap () and fill (), as well as 1 Textwrapper, the class of does all the work, and the utility func tions, dedent () and indent (). If 2 You ' re just wrapping or filling one or both text strings, the convenience functions should be good 3 enough; o Therwise, you should a instance of Textwrapper for efficiency. 4 " print (Wrap (TEST_STR)) def main (): test_wrap () if __name__ = = ' __main__ ': Main ()
Output effect:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> [' The textwrap ', ' module provides ', ' convenience ', ' functions, wrap () ', ' and fill (), as well ', ' As 1 ', ' textwrapper, the ', ' class that does all ', ' the work, and ', ' utility functions, ', ' dedent () and ', ' Indent (). If 2 ', ' You ' re just wrapping ', ' or filling one or ', ' both text strings, ', ' The convenience ', ' functions should be ', ' good 3 enough; ', ' Otherwise, you ', ' should use ', ' instance of ', ' textwrapper for ', ' efficiency. 4 ']>>>
We will find that the wrap () function splits the string into a sequence in which the length of each element is the same.
Fill (text, width=70, **kwargs): The method splits the string according to the specified length, and then displays it row by line
From textwrap Import * #fill () method Def test_wrap (): test_str = "\ The textwrap module provides the convenience funct Ions, wrap () and fill (), as well as 1 Textwrapper, the class that does all the work, and the utility functions, dedent () and indent (). If 2 You ' re just wrapping or filling one or both text strings, the convenience functions should be good 3 enough; o Therwise, you should a instance of Textwrapper for efficiency. 4 ' print (Fill (TEST_STR, max)) def main (): test_wrap () if __name__ = = ' __main__ ': Main ()
Operating effect:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> the textwrap module provides twoconvenience functions, wrap () Andfill (), as well as 1 Textwrapp Er,the class that does all the work, Andtwo utility functions, Dedent () andindent (). If 2 You ' re just wrappingor filling one or both text strings, theconvenience functions should be good 3enough; Otherwis E, you should use aninstance of Textwrapper for efficiency.>>>
Dedent () method, the text is not indented, and the corresponding indent () method is indented to display
From textwrap Import * #dedent () method Def test_wrap (): test_str = "\ The textwrap module provides the CONVENIENCE
functions, wrap () and fill (), as well as 1 Textwrapper, the class that does all the work, and the utility Functi ONS, dedent () and indent (). If 2 You ' re just wrapping or filling one or both text strings, the convenience functions should be good 3 enoug H Otherwise, should use of the instance of textwrapper for efficiency. 4 " print (Repr (dedent (TEST_STR))) def main (): test_wrap () if __name__ = = ' __main__ ': Main ()
Operating effect:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> ' The TextWrap module provides-convenience\n functions, wrap () and fill (), as well as 1\ntextwrap Per, the class that does all the work,\n and the utility functions, dedent () and indent (). If 2\nyou ' re just wrapping or filling one or both text strings,\n the convenience functions should be good 3\nenough; o Therwise, you should use a instance\n of textwrapper for efficiency. 4\n ' >>>
I hope this article is helpful for Python program design.