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