Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame^] | 1 | .. _tut-io: |
| 2 | |
| 3 | **************** |
| 4 | Input and Output |
| 5 | **************** |
| 6 | |
| 7 | There are several ways to present the output of a program; data can be printed |
| 8 | in a human-readable form, or written to a file for future use. This chapter will |
| 9 | discuss some of the possibilities. |
| 10 | |
| 11 | |
| 12 | .. _tut-formatting: |
| 13 | |
| 14 | Fancier Output Formatting |
| 15 | ========================= |
| 16 | |
| 17 | So far we've encountered two ways of writing values: *expression statements* and |
| 18 | the :keyword:`print` statement. (A third way is using the :meth:`write` method |
| 19 | of file objects; the standard output file can be referenced as ``sys.stdout``. |
| 20 | See the Library Reference for more information on this.) |
| 21 | |
| 22 | .. index:: module: string |
| 23 | |
| 24 | Often you'll want more control over the formatting of your output than simply |
| 25 | printing space-separated values. There are two ways to format your output; the |
| 26 | first way is to do all the string handling yourself; using string slicing and |
| 27 | concatenation operations you can create any layout you can imagine. The |
| 28 | standard module :mod:`string` contains some useful operations for padding |
| 29 | strings to a given column width; these will be discussed shortly. The second |
| 30 | way is to use the ``%`` operator with a string as the left argument. The ``%`` |
| 31 | operator interprets the left argument much like a :cfunc:`sprintf`\ -style |
| 32 | format string to be applied to the right argument, and returns the string |
| 33 | resulting from this formatting operation. |
| 34 | |
| 35 | One question remains, of course: how do you convert values to strings? Luckily, |
| 36 | Python has ways to convert any value to a string: pass it to the :func:`repr` |
| 37 | or :func:`str` functions. Reverse quotes (``````) are equivalent to |
| 38 | :func:`repr`, but they are no longer used in modern Python code and will likely |
| 39 | not be in future versions of the language. |
| 40 | |
| 41 | The :func:`str` function is meant to return representations of values which are |
| 42 | fairly human-readable, while :func:`repr` is meant to generate representations |
| 43 | which can be read by the interpreter (or will force a :exc:`SyntaxError` if |
| 44 | there is not equivalent syntax). For objects which don't have a particular |
| 45 | representation for human consumption, :func:`str` will return the same value as |
| 46 | :func:`repr`. Many values, such as numbers or structures like lists and |
| 47 | dictionaries, have the same representation using either function. Strings and |
| 48 | floating point numbers, in particular, have two distinct representations. |
| 49 | |
| 50 | Some examples:: |
| 51 | |
| 52 | >>> s = 'Hello, world.' |
| 53 | >>> str(s) |
| 54 | 'Hello, world.' |
| 55 | >>> repr(s) |
| 56 | "'Hello, world.'" |
| 57 | >>> str(0.1) |
| 58 | '0.1' |
| 59 | >>> repr(0.1) |
| 60 | '0.10000000000000001' |
| 61 | >>> x = 10 * 3.25 |
| 62 | >>> y = 200 * 200 |
| 63 | >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...' |
| 64 | >>> print s |
| 65 | The value of x is 32.5, and y is 40000... |
| 66 | >>> # The repr() of a string adds string quotes and backslashes: |
| 67 | ... hello = 'hello, world\n' |
| 68 | >>> hellos = repr(hello) |
| 69 | >>> print hellos |
| 70 | 'hello, world\n' |
| 71 | >>> # The argument to repr() may be any Python object: |
| 72 | ... repr((x, y, ('spam', 'eggs'))) |
| 73 | "(32.5, 40000, ('spam', 'eggs'))" |
| 74 | >>> # reverse quotes are convenient in interactive sessions: |
| 75 | ... `x, y, ('spam', 'eggs')` |
| 76 | "(32.5, 40000, ('spam', 'eggs'))" |
| 77 | |
| 78 | Here are two ways to write a table of squares and cubes:: |
| 79 | |
| 80 | >>> for x in range(1, 11): |
| 81 | ... print repr(x).rjust(2), repr(x*x).rjust(3), |
| 82 | ... # Note trailing comma on previous line |
| 83 | ... print repr(x*x*x).rjust(4) |
| 84 | ... |
| 85 | 1 1 1 |
| 86 | 2 4 8 |
| 87 | 3 9 27 |
| 88 | 4 16 64 |
| 89 | 5 25 125 |
| 90 | 6 36 216 |
| 91 | 7 49 343 |
| 92 | 8 64 512 |
| 93 | 9 81 729 |
| 94 | 10 100 1000 |
| 95 | |
| 96 | >>> for x in range(1,11): |
| 97 | ... print '%2d %3d %4d' % (x, x*x, x*x*x) |
| 98 | ... |
| 99 | 1 1 1 |
| 100 | 2 4 8 |
| 101 | 3 9 27 |
| 102 | 4 16 64 |
| 103 | 5 25 125 |
| 104 | 6 36 216 |
| 105 | 7 49 343 |
| 106 | 8 64 512 |
| 107 | 9 81 729 |
| 108 | 10 100 1000 |
| 109 | |
| 110 | (Note that in the first example, one space between each column was added by the |
| 111 | way :keyword:`print` works: it always adds spaces between its arguments.) |
| 112 | |
| 113 | This example demonstrates the :meth:`rjust` method of string objects, which |
| 114 | right-justifies a string in a field of a given width by padding it with spaces |
| 115 | on the left. There are similar methods :meth:`ljust` and :meth:`center`. These |
| 116 | methods do not write anything, they just return a new string. If the input |
| 117 | string is too long, they don't truncate it, but return it unchanged; this will |
| 118 | mess up your column lay-out but that's usually better than the alternative, |
| 119 | which would be lying about a value. (If you really want truncation you can |
| 120 | always add a slice operation, as in ``x.ljust(n)[:n]``.) |
| 121 | |
| 122 | There is another method, :meth:`zfill`, which pads a numeric string on the left |
| 123 | with zeros. It understands about plus and minus signs:: |
| 124 | |
| 125 | >>> '12'.zfill(5) |
| 126 | '00012' |
| 127 | >>> '-3.14'.zfill(7) |
| 128 | '-003.14' |
| 129 | >>> '3.14159265359'.zfill(5) |
| 130 | '3.14159265359' |
| 131 | |
| 132 | Using the ``%`` operator looks like this:: |
| 133 | |
| 134 | >>> import math |
| 135 | >>> print 'The value of PI is approximately %5.3f.' % math.pi |
| 136 | The value of PI is approximately 3.142. |
| 137 | |
| 138 | If there is more than one format in the string, you need to pass a tuple as |
| 139 | right operand, as in this example:: |
| 140 | |
| 141 | >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} |
| 142 | >>> for name, phone in table.items(): |
| 143 | ... print '%-10s ==> %10d' % (name, phone) |
| 144 | ... |
| 145 | Jack ==> 4098 |
| 146 | Dcab ==> 7678 |
| 147 | Sjoerd ==> 4127 |
| 148 | |
| 149 | Most formats work exactly as in C and require that you pass the proper type; |
| 150 | however, if you don't you get an exception, not a core dump. The ``%s`` format |
| 151 | is more relaxed: if the corresponding argument is not a string object, it is |
| 152 | converted to string using the :func:`str` built-in function. Using ``*`` to |
| 153 | pass the width or precision in as a separate (integer) argument is supported. |
| 154 | The C formats ``%n`` and ``%p`` are not supported. |
| 155 | |
| 156 | If you have a really long format string that you don't want to split up, it |
| 157 | would be nice if you could reference the variables to be formatted by name |
| 158 | instead of by position. This can be done by using form ``%(name)format``, as |
| 159 | shown here:: |
| 160 | |
| 161 | >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} |
| 162 | >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table |
| 163 | Jack: 4098; Sjoerd: 4127; Dcab: 8637678 |
| 164 | |
| 165 | This is particularly useful in combination with the new built-in :func:`vars` |
| 166 | function, which returns a dictionary containing all local variables. |
| 167 | |
| 168 | |
| 169 | .. _tut-files: |
| 170 | |
| 171 | Reading and Writing Files |
| 172 | ========================= |
| 173 | |
| 174 | .. index:: |
| 175 | builtin: open |
| 176 | object: file |
| 177 | |
| 178 | :func:`open` returns a file object, and is most commonly used with two |
| 179 | arguments: ``open(filename, mode)``. |
| 180 | |
| 181 | .. % Opening files |
| 182 | |
| 183 | :: |
| 184 | |
| 185 | >>> f=open('/tmp/workfile', 'w') |
| 186 | >>> print f |
| 187 | <open file '/tmp/workfile', mode 'w' at 80a0960> |
| 188 | |
| 189 | The first argument is a string containing the filename. The second argument is |
| 190 | another string containing a few characters describing the way in which the file |
| 191 | will be used. *mode* can be ``'r'`` when the file will only be read, ``'w'`` |
| 192 | for only writing (an existing file with the same name will be erased), and |
| 193 | ``'a'`` opens the file for appending; any data written to the file is |
| 194 | automatically added to the end. ``'r+'`` opens the file for both reading and |
| 195 | writing. The *mode* argument is optional; ``'r'`` will be assumed if it's |
| 196 | omitted. |
| 197 | |
| 198 | On Windows and the Macintosh, ``'b'`` appended to the mode opens the file in |
| 199 | binary mode, so there are also modes like ``'rb'``, ``'wb'``, and ``'r+b'``. |
| 200 | Windows makes a distinction between text and binary files; the end-of-line |
| 201 | characters in text files are automatically altered slightly when data is read or |
| 202 | written. This behind-the-scenes modification to file data is fine for ASCII |
| 203 | text files, but it'll corrupt binary data like that in :file:`JPEG` or |
| 204 | :file:`EXE` files. Be very careful to use binary mode when reading and writing |
| 205 | such files. |
| 206 | |
| 207 | |
| 208 | .. _tut-filemethods: |
| 209 | |
| 210 | Methods of File Objects |
| 211 | ----------------------- |
| 212 | |
| 213 | The rest of the examples in this section will assume that a file object called |
| 214 | ``f`` has already been created. |
| 215 | |
| 216 | To read a file's contents, call ``f.read(size)``, which reads some quantity of |
| 217 | data and returns it as a string. *size* is an optional numeric argument. When |
| 218 | *size* is omitted or negative, the entire contents of the file will be read and |
| 219 | returned; it's your problem if the file is twice as large as your machine's |
| 220 | memory. Otherwise, at most *size* bytes are read and returned. If the end of |
| 221 | the file has been reached, ``f.read()`` will return an empty string (``""``). |
| 222 | :: |
| 223 | |
| 224 | >>> f.read() |
| 225 | 'This is the entire file.\n' |
| 226 | >>> f.read() |
| 227 | '' |
| 228 | |
| 229 | ``f.readline()`` reads a single line from the file; a newline character (``\n``) |
| 230 | is left at the end of the string, and is only omitted on the last line of the |
| 231 | file if the file doesn't end in a newline. This makes the return value |
| 232 | unambiguous; if ``f.readline()`` returns an empty string, the end of the file |
| 233 | has been reached, while a blank line is represented by ``'\n'``, a string |
| 234 | containing only a single newline. :: |
| 235 | |
| 236 | >>> f.readline() |
| 237 | 'This is the first line of the file.\n' |
| 238 | >>> f.readline() |
| 239 | 'Second line of the file\n' |
| 240 | >>> f.readline() |
| 241 | '' |
| 242 | |
| 243 | ``f.readlines()`` returns a list containing all the lines of data in the file. |
| 244 | If given an optional parameter *sizehint*, it reads that many bytes from the |
| 245 | file and enough more to complete a line, and returns the lines from that. This |
| 246 | is often used to allow efficient reading of a large file by lines, but without |
| 247 | having to load the entire file in memory. Only complete lines will be returned. |
| 248 | :: |
| 249 | |
| 250 | >>> f.readlines() |
| 251 | ['This is the first line of the file.\n', 'Second line of the file\n'] |
| 252 | |
| 253 | An alternate approach to reading lines is to loop over the file object. This is |
| 254 | memory efficient, fast, and leads to simpler code:: |
| 255 | |
| 256 | >>> for line in f: |
| 257 | print line, |
| 258 | |
| 259 | This is the first line of the file. |
| 260 | Second line of the file |
| 261 | |
| 262 | The alternative approach is simpler but does not provide as fine-grained |
| 263 | control. Since the two approaches manage line buffering differently, they |
| 264 | should not be mixed. |
| 265 | |
| 266 | ``f.write(string)`` writes the contents of *string* to the file, returning |
| 267 | ``None``. :: |
| 268 | |
| 269 | >>> f.write('This is a test\n') |
| 270 | |
| 271 | To write something other than a string, it needs to be converted to a string |
| 272 | first:: |
| 273 | |
| 274 | >>> value = ('the answer', 42) |
| 275 | >>> s = str(value) |
| 276 | >>> f.write(s) |
| 277 | |
| 278 | ``f.tell()`` returns an integer giving the file object's current position in the |
| 279 | file, measured in bytes from the beginning of the file. To change the file |
| 280 | object's position, use ``f.seek(offset, from_what)``. The position is computed |
| 281 | from adding *offset* to a reference point; the reference point is selected by |
| 282 | the *from_what* argument. A *from_what* value of 0 measures from the beginning |
| 283 | of the file, 1 uses the current file position, and 2 uses the end of the file as |
| 284 | the reference point. *from_what* can be omitted and defaults to 0, using the |
| 285 | beginning of the file as the reference point. :: |
| 286 | |
| 287 | >>> f = open('/tmp/workfile', 'r+') |
| 288 | >>> f.write('0123456789abcdef') |
| 289 | >>> f.seek(5) # Go to the 6th byte in the file |
| 290 | >>> f.read(1) |
| 291 | '5' |
| 292 | >>> f.seek(-3, 2) # Go to the 3rd byte before the end |
| 293 | >>> f.read(1) |
| 294 | 'd' |
| 295 | |
| 296 | When you're done with a file, call ``f.close()`` to close it and free up any |
| 297 | system resources taken up by the open file. After calling ``f.close()``, |
| 298 | attempts to use the file object will automatically fail. :: |
| 299 | |
| 300 | >>> f.close() |
| 301 | >>> f.read() |
| 302 | Traceback (most recent call last): |
| 303 | File "<stdin>", line 1, in ? |
| 304 | ValueError: I/O operation on closed file |
| 305 | |
| 306 | File objects have some additional methods, such as :meth:`isatty` and |
| 307 | :meth:`truncate` which are less frequently used; consult the Library Reference |
| 308 | for a complete guide to file objects. |
| 309 | |
| 310 | |
| 311 | .. _tut-pickle: |
| 312 | |
| 313 | The :mod:`pickle` Module |
| 314 | ------------------------ |
| 315 | |
| 316 | .. index:: module: pickle |
| 317 | |
| 318 | Strings can easily be written to and read from a file. Numbers take a bit more |
| 319 | effort, since the :meth:`read` method only returns strings, which will have to |
| 320 | be passed to a function like :func:`int`, which takes a string like ``'123'`` |
| 321 | and returns its numeric value 123. However, when you want to save more complex |
| 322 | data types like lists, dictionaries, or class instances, things get a lot more |
| 323 | complicated. |
| 324 | |
| 325 | Rather than have users be constantly writing and debugging code to save |
| 326 | complicated data types, Python provides a standard module called :mod:`pickle`. |
| 327 | This is an amazing module that can take almost any Python object (even some |
| 328 | forms of Python code!), and convert it to a string representation; this process |
| 329 | is called :dfn:`pickling`. Reconstructing the object from the string |
| 330 | representation is called :dfn:`unpickling`. Between pickling and unpickling, |
| 331 | the string representing the object may have been stored in a file or data, or |
| 332 | sent over a network connection to some distant machine. |
| 333 | |
| 334 | If you have an object ``x``, and a file object ``f`` that's been opened for |
| 335 | writing, the simplest way to pickle the object takes only one line of code:: |
| 336 | |
| 337 | pickle.dump(x, f) |
| 338 | |
| 339 | To unpickle the object again, if ``f`` is a file object which has been opened |
| 340 | for reading:: |
| 341 | |
| 342 | x = pickle.load(f) |
| 343 | |
| 344 | (There are other variants of this, used when pickling many objects or when you |
| 345 | don't want to write the pickled data to a file; consult the complete |
| 346 | documentation for :mod:`pickle` in the Python Library Reference.) |
| 347 | |
| 348 | :mod:`pickle` is the standard way to make Python objects which can be stored and |
| 349 | reused by other programs or by a future invocation of the same program; the |
| 350 | technical term for this is a :dfn:`persistent` object. Because :mod:`pickle` is |
| 351 | so widely used, many authors who write Python extensions take care to ensure |
| 352 | that new data types such as matrices can be properly pickled and unpickled. |
| 353 | |
| 354 | |