| 1 |
:mod:`textwrap` --- Text wrapping and filling |
| 2 |
============================================= |
| 3 |
|
| 4 |
.. module:: textwrap |
| 5 |
:synopsis: Text wrapping and filling |
| 6 |
.. moduleauthor:: Greg Ward <[email protected]> |
| 7 |
.. sectionauthor:: Greg Ward <[email protected]> |
| 8 |
|
| 9 |
.. versionadded:: 2.3 |
| 10 |
|
| 11 |
**Source code:** :source:`Lib/textwrap.py` |
| 12 |
|
| 13 |
-------------- |
| 14 |
|
| 15 |
The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and |
| 16 |
:func:`fill`, as well as :class:`TextWrapper`, the class that does all the work, |
| 17 |
and a utility function :func:`dedent`. If you're just wrapping or filling one |
| 18 |
or two text strings, the convenience functions should be good enough; |
| 19 |
otherwise, you should use an instance of :class:`TextWrapper` for efficiency. |
| 20 |
|
| 21 |
.. function:: wrap(text[, width[, ...]]) |
| 22 |
|
| 23 |
Wraps the single paragraph in *text* (a string) so every line is at most *width* |
| 24 |
characters long. Returns a list of output lines, without final newlines. |
| 25 |
|
| 26 |
Optional keyword arguments correspond to the instance attributes of |
| 27 |
:class:`TextWrapper`, documented below. *width* defaults to ``70``. |
| 28 |
|
| 29 |
See the :meth:`TextWrapper.wrap` method for additional details on how |
| 30 |
:func:`wrap` behaves. |
| 31 |
|
| 32 |
|
| 33 |
.. function:: fill(text[, width[, ...]]) |
| 34 |
|
| 35 |
Wraps the single paragraph in *text*, and returns a single string containing the |
| 36 |
wrapped paragraph. :func:`fill` is shorthand for :: |
| 37 |
|
| 38 |
"\n".join(wrap(text, ...)) |
| 39 |
|
| 40 |
In particular, :func:`fill` accepts exactly the same keyword arguments as |
| 41 |
:func:`wrap`. |
| 42 |
|
| 43 |
Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper` |
| 44 |
instance and calling a single method on it. That instance is not reused, so for |
| 45 |
applications that wrap/fill many text strings, it will be more efficient for you |
| 46 |
to create your own :class:`TextWrapper` object. |
| 47 |
|
| 48 |
Text is preferably wrapped on whitespaces and right after the hyphens in |
| 49 |
hyphenated words; only then will long words be broken if necessary, unless |
| 50 |
:attr:`TextWrapper.break_long_words` is set to false. |
| 51 |
|
| 52 |
An additional utility function, :func:`dedent`, is provided to remove |
| 53 |
indentation from strings that have unwanted whitespace to the left of the text. |
| 54 |
|
| 55 |
|
| 56 |
.. function:: dedent(text) |
| 57 |
|
| 58 |
Remove any common leading whitespace from every line in *text*. |
| 59 |
|
| 60 |
This can be used to make triple-quoted strings line up with the left edge of the |
| 61 |
display, while still presenting them in the source code in indented form. |
| 62 |
|
| 63 |
Note that tabs and spaces are both treated as whitespace, but they are not |
| 64 |
equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no |
| 65 |
common leading whitespace. (This behaviour is new in Python 2.5; older versions |
| 66 |
of this module incorrectly expanded tabs before searching for common leading |
| 67 |
whitespace.) |
| 68 |
|
| 69 |
Lines containing only whitespace are ignored in the input and normalized to a |
| 70 |
single newline character in the output. |
| 71 |
|
| 72 |
For example:: |
| 73 |
|
| 74 |
def test(): |
| 75 |
# end first line with \ to avoid the empty line! |
| 76 |
s = '''\ |
| 77 |
hello |
| 78 |
world |
| 79 |
''' |
| 80 |
print repr(s) # prints ' hello\n world\n ' |
| 81 |
print repr(dedent(s)) # prints 'hello\n world\n' |
| 82 |
|
| 83 |
|
| 84 |
.. class:: TextWrapper(...) |
| 85 |
|
| 86 |
The :class:`TextWrapper` constructor accepts a number of optional keyword |
| 87 |
arguments. Each argument corresponds to one instance attribute, so for example |
| 88 |
:: |
| 89 |
|
| 90 |
wrapper = TextWrapper(initial_indent="* ") |
| 91 |
|
| 92 |
is the same as :: |
| 93 |
|
| 94 |
wrapper = TextWrapper() |
| 95 |
wrapper.initial_indent = "* " |
| 96 |
|
| 97 |
You can re-use the same :class:`TextWrapper` object many times, and you can |
| 98 |
change any of its options through direct assignment to instance attributes |
| 99 |
between uses. |
| 100 |
|
| 101 |
The :class:`TextWrapper` instance attributes (and keyword arguments to the |
| 102 |
constructor) are as follows: |
| 103 |
|
| 104 |
|
| 105 |
.. attribute:: width |
| 106 |
|
| 107 |
(default: ``70``) The maximum length of wrapped lines. As long as there |
| 108 |
are no individual words in the input text longer than :attr:`width`, |
| 109 |
:class:`TextWrapper` guarantees that no output line will be longer than |
| 110 |
:attr:`width` characters. |
| 111 |
|
| 112 |
|
| 113 |
.. attribute:: expand_tabs |
| 114 |
|
| 115 |
(default: ``True``) If true, then all tab characters in *text* will be |
| 116 |
expanded to spaces using the :meth:`expandtabs` method of *text*. |
| 117 |
|
| 118 |
|
| 119 |
.. attribute:: replace_whitespace |
| 120 |
|
| 121 |
(default: ``True``) If true, after tab expansion but before wrapping, |
| 122 |
the :meth:`wrap` method will replace each whitespace character |
| 123 |
with a single space. The whitespace characters replaced are |
| 124 |
as follows: tab, newline, vertical tab, formfeed, and carriage |
| 125 |
return (``'\t\n\v\f\r'``). |
| 126 |
|
| 127 |
.. note:: |
| 128 |
|
| 129 |
If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, |
| 130 |
each tab character will be replaced by a single space, which is *not* |
| 131 |
the same as tab expansion. |
| 132 |
|
| 133 |
.. note:: |
| 134 |
|
| 135 |
If :attr:`replace_whitespace` is false, newlines may appear in the |
| 136 |
middle of a line and cause strange output. For this reason, text should |
| 137 |
be split into paragraphs (using :meth:`str.splitlines` or similar) |
| 138 |
which are wrapped separately. |
| 139 |
|
| 140 |
|
| 141 |
.. attribute:: drop_whitespace |
| 142 |
|
| 143 |
(default: ``True``) If true, whitespace at the beginning and ending of |
| 144 |
every line (after wrapping but before indenting) is dropped. |
| 145 |
Whitespace at the beginning of the paragraph, however, is not dropped |
| 146 |
if non-whitespace follows it. If whitespace being dropped takes up an |
| 147 |
entire line, the whole line is dropped. |
| 148 |
|
| 149 |
.. versionadded:: 2.6 |
| 150 |
Whitespace was always dropped in earlier versions. |
| 151 |
|
| 152 |
|
| 153 |
.. attribute:: initial_indent |
| 154 |
|
| 155 |
(default: ``''``) String that will be prepended to the first line of |
| 156 |
wrapped output. Counts towards the length of the first line. The empty |
| 157 |
string is not indented. |
| 158 |
|
| 159 |
|
| 160 |
.. attribute:: subsequent_indent |
| 161 |
|
| 162 |
(default: ``''``) String that will be prepended to all lines of wrapped |
| 163 |
output except the first. Counts towards the length of each line except |
| 164 |
the first. |
| 165 |
|
| 166 |
|
| 167 |
.. attribute:: fix_sentence_endings |
| 168 |
|
| 169 |
(default: ``False``) If true, :class:`TextWrapper` attempts to detect |
| 170 |
sentence endings and ensure that sentences are always separated by exactly |
| 171 |
two spaces. This is generally desired for text in a monospaced font. |
| 172 |
However, the sentence detection algorithm is imperfect: it assumes that a |
| 173 |
sentence ending consists of a lowercase letter followed by one of ``'.'``, |
| 174 |
``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``, |
| 175 |
followed by a space. One problem with this is algorithm is that it is |
| 176 |
unable to detect the difference between "Dr." in :: |
| 177 |
|
| 178 |
[...] Dr. Frankenstein's monster [...] |
| 179 |
|
| 180 |
and "Spot." in :: |
| 181 |
|
| 182 |
[...] See Spot. See Spot run [...] |
| 183 |
|
| 184 |
:attr:`fix_sentence_endings` is false by default. |
| 185 |
|
| 186 |
Since the sentence detection algorithm relies on ``string.lowercase`` for |
| 187 |
the definition of "lowercase letter," and a convention of using two spaces |
| 188 |
after a period to separate sentences on the same line, it is specific to |
| 189 |
English-language texts. |
| 190 |
|
| 191 |
|
| 192 |
.. attribute:: break_long_words |
| 193 |
|
| 194 |
(default: ``True``) If true, then words longer than :attr:`width` will be |
| 195 |
broken in order to ensure that no lines are longer than :attr:`width`. If |
| 196 |
it is false, long words will not be broken, and some lines may be longer |
| 197 |
than :attr:`width`. (Long words will be put on a line by themselves, in |
| 198 |
order to minimize the amount by which :attr:`width` is exceeded.) |
| 199 |
|
| 200 |
|
| 201 |
.. attribute:: break_on_hyphens |
| 202 |
|
| 203 |
(default: ``True``) If true, wrapping will occur preferably on whitespaces |
| 204 |
and right after hyphens in compound words, as it is customary in English. |
| 205 |
If false, only whitespaces will be considered as potentially good places |
| 206 |
for line breaks, but you need to set :attr:`break_long_words` to false if |
| 207 |
you want truly insecable words. Default behaviour in previous versions |
| 208 |
was to always allow breaking hyphenated words. |
| 209 |
|
| 210 |
.. versionadded:: 2.6 |
| 211 |
|
| 212 |
|
| 213 |
:class:`TextWrapper` also provides two public methods, analogous to the |
| 214 |
module-level convenience functions: |
| 215 |
|
| 216 |
.. method:: wrap(text) |
| 217 |
|
| 218 |
Wraps the single paragraph in *text* (a string) so every line is at most |
| 219 |
:attr:`width` characters long. All wrapping options are taken from |
| 220 |
instance attributes of the :class:`TextWrapper` instance. Returns a list |
| 221 |
of output lines, without final newlines. If the wrapped output has no |
| 222 |
content, the returned list is empty. |
| 223 |
|
| 224 |
|
| 225 |
.. method:: fill(text) |
| 226 |
|
| 227 |
Wraps the single paragraph in *text*, and returns a single string |
| 228 |
containing the wrapped paragraph. |
| 229 |
|