Home > Article > Backend Development > textwrap text wrapping and filling
Python module learning - textwrap text wrapping and filling
Code example:
sample_text = '''
The textwrap module can be used to format text for output in
situations wherepretty-printing is desired. It offers
similar programmatic functionality to the paragraph wrapping
or filling features found in many text editors.
'''
paragraph filling:
import textwrap from textwrap_exampleimport sample_text print 'Nodedent:\n' printtextwrap.fill(sample_text, width=50)
Execution result:
# 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
functionality similar to the paragraph wrapping
or fillingfeatures found in many text editors.
The result is Left aligned, first line indented. Spaces in the line are retained.
Remove indentation:
import textwrap fromtextwrap_example import sample_text dedented_text = textwrap.dedent(sample_text) print 'Dedented:' printdedented_text
Execution result:
# pythontextwrap_dedent.py
Dedented:
The textwrapmodule can be used to format text for output in
situations wherepretty -printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or fillingfeatures found in many text editors.
so that the first line is not indented.
Combined removal of indentation and padding:
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
Execution result:
# 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 textwrapmod ule can be used to format text for output in
situations where pretty-printing is desired. It offers programmatic
functionality similar to the paragraph wrapping or filling features
found in many texteditors.
Hanging indent: Hanging indent The indentation of the first line is less than Indentation of other lines.
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.
The ''*4 can also be replaced by other characters.
TextWrap provides the functions wrap() and fill(), as well as the TextWrapper class and the tool function dedent(). Usually wrapping or filling one or two strings uses wrap() and fill(). In other cases it is more efficient to use TextWrapper.
textwrap.wrap(text[,width[, ...]])
Wraps a single paragraph (text is input, system string), and the maximum width of each line is width. Returns a list of output lines, with the last line without a newline character. Width defaults to 70.
textwrap.fill(text[,width[, ...]])
Wraps a single paragraph of text and returns a string containing the wrapped paragraph. It is actually the abbreviation of "n".join(wrap(text,...)). wrap() and fill() create TextWrapper instances and call a method. These instances are not reused, so to wrap/populate many text strings it is more efficient to construct your own TextWrapper objects. TextWrapper.break_long_words sets whether to break long words.
textwrap.dedent(text)
Dedent removes the white space at the beginning of each line. This facilitates displaying content enclosed in triple quotes without modifying the indentation in its source code.