blob: 5314fedb6df9574ab87e22223fc9bc55bb72d13b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _tut-io:
2
3****************
4Input and Output
5****************
6
7There are several ways to present the output of a program; data can be printed
8in a human-readable form, or written to a file for future use. This chapter will
9discuss some of the possibilities.
10
11
12.. _tut-formatting:
13
14Fancier Output Formatting
15=========================
16
17So far we've encountered two ways of writing values: *expression statements* and
Guido van Rossum0616b792007-08-31 03:25:11 +000018the :func:`print` function. (A third way is using the :meth:`write` method
Georg Brandl116aa622007-08-15 14:28:22 +000019of file objects; the standard output file can be referenced as ``sys.stdout``.
20See the Library Reference for more information on this.)
21
Georg Brandl116aa622007-08-15 14:28:22 +000022Often you'll want more control over the formatting of your output than simply
23printing space-separated values. There are two ways to format your output; the
24first way is to do all the string handling yourself; using string slicing and
25concatenation operations you can create any layout you can imagine. The
Georg Brandl3640e182011-03-06 10:56:18 +010026string type has some methods that perform useful operations for padding
Georg Brandl116aa622007-08-15 14:28:22 +000027strings to a given column width; these will be discussed shortly. The second
Benjamin Petersone6f00632008-05-26 01:03:56 +000028way is to use the :meth:`str.format` method.
29
Georg Brandl3640e182011-03-06 10:56:18 +010030The :mod:`string` module contains a :class:`~string.Template` class which offers
31yet another way to substitute values into strings.
Georg Brandl116aa622007-08-15 14:28:22 +000032
33One question remains, of course: how do you convert values to strings? Luckily,
34Python has ways to convert any value to a string: pass it to the :func:`repr`
Georg Brandl1e3830a2008-08-08 06:45:01 +000035or :func:`str` functions.
Georg Brandl116aa622007-08-15 14:28:22 +000036
37The :func:`str` function is meant to return representations of values which are
38fairly human-readable, while :func:`repr` is meant to generate representations
39which can be read by the interpreter (or will force a :exc:`SyntaxError` if
Sandro Tosia17ef142012-08-14 19:51:43 +020040there is no equivalent syntax). For objects which don't have a particular
Georg Brandl116aa622007-08-15 14:28:22 +000041representation for human consumption, :func:`str` will return the same value as
42:func:`repr`. Many values, such as numbers or structures like lists and
Ezio Melotti0def5c62011-03-13 02:27:26 +020043dictionaries, have the same representation using either function. Strings, in
44particular, have two distinct representations.
Georg Brandl116aa622007-08-15 14:28:22 +000045
46Some examples::
47
48 >>> s = 'Hello, world.'
49 >>> str(s)
50 'Hello, world.'
51 >>> repr(s)
52 "'Hello, world.'"
Ezio Melotti0def5c62011-03-13 02:27:26 +020053 >>> str(1/7)
Mark Dickinson5a55b612009-06-28 20:59:42 +000054 '0.14285714285714285'
Georg Brandl116aa622007-08-15 14:28:22 +000055 >>> x = 10 * 3.25
56 >>> y = 200 * 200
57 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
Guido van Rossum0616b792007-08-31 03:25:11 +000058 >>> print(s)
Georg Brandl116aa622007-08-15 14:28:22 +000059 The value of x is 32.5, and y is 40000...
60 >>> # The repr() of a string adds string quotes and backslashes:
61 ... hello = 'hello, world\n'
62 >>> hellos = repr(hello)
Guido van Rossum0616b792007-08-31 03:25:11 +000063 >>> print(hellos)
Georg Brandl116aa622007-08-15 14:28:22 +000064 'hello, world\n'
65 >>> # The argument to repr() may be any Python object:
66 ... repr((x, y, ('spam', 'eggs')))
67 "(32.5, 40000, ('spam', 'eggs'))"
Georg Brandl116aa622007-08-15 14:28:22 +000068
69Here are two ways to write a table of squares and cubes::
70
71 >>> for x in range(1, 11):
Georg Brandle4ac7502007-09-03 07:10:24 +000072 ... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
Guido van Rossum0616b792007-08-31 03:25:11 +000073 ... # Note use of 'end' on previous line
74 ... print(repr(x*x*x).rjust(4))
Georg Brandl116aa622007-08-15 14:28:22 +000075 ...
76 1 1 1
77 2 4 8
78 3 9 27
79 4 16 64
80 5 25 125
81 6 36 216
82 7 49 343
83 8 64 512
84 9 81 729
85 10 100 1000
86
Georg Brandle4ac7502007-09-03 07:10:24 +000087 >>> for x in range(1, 11):
Benjamin Petersone6f00632008-05-26 01:03:56 +000088 ... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
Georg Brandl48310cd2009-01-03 21:18:54 +000089 ...
Georg Brandl116aa622007-08-15 14:28:22 +000090 1 1 1
91 2 4 8
92 3 9 27
93 4 16 64
94 5 25 125
95 6 36 216
96 7 49 343
97 8 64 512
98 9 81 729
99 10 100 1000
100
101(Note that in the first example, one space between each column was added by the
Guido van Rossum0616b792007-08-31 03:25:11 +0000102way :func:`print` works: it always adds spaces between its arguments.)
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Ezio Melotti2b736602011-03-13 02:19:57 +0200104This example demonstrates the :meth:`str.rjust` method of string
105objects, which right-justifies a string in a field of a given width by padding
106it with spaces on the left. There are similar methods :meth:`str.ljust` and
107:meth:`str.center`. These methods do not write anything, they just return a
108new string. If the input string is too long, they don't truncate it, but
109return it unchanged; this will mess up your column lay-out but that's usually
110better than the alternative, which would be lying about a value. (If you
111really want truncation you can always add a slice operation, as in
112``x.ljust(n)[:n]``.)
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Ezio Melotti2b736602011-03-13 02:19:57 +0200114There is another method, :meth:`str.zfill`, which pads a numeric string on the
115left with zeros. It understands about plus and minus signs::
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117 >>> '12'.zfill(5)
118 '00012'
119 >>> '-3.14'.zfill(7)
120 '-003.14'
121 >>> '3.14159265359'.zfill(5)
122 '3.14159265359'
123
Benjamin Petersone6f00632008-05-26 01:03:56 +0000124Basic usage of the :meth:`str.format` method looks like this::
125
Georg Brandl2f3ed682009-09-01 07:42:40 +0000126 >>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
Benjamin Petersone6f00632008-05-26 01:03:56 +0000127 We are the knights who say "Ni!"
128
129The brackets and characters within them (called format fields) are replaced with
Ezio Melotti2b736602011-03-13 02:19:57 +0200130the objects passed into the :meth:`str.format` method. A number in the
Georg Brandl2f3ed682009-09-01 07:42:40 +0000131brackets can be used to refer to the position of the object passed into the
Ezio Melotti2b736602011-03-13 02:19:57 +0200132:meth:`str.format` method. ::
Benjamin Petersone6f00632008-05-26 01:03:56 +0000133
Benjamin Peterson0cea1572008-07-26 21:59:03 +0000134 >>> print('{0} and {1}'.format('spam', 'eggs'))
Benjamin Petersone6f00632008-05-26 01:03:56 +0000135 spam and eggs
Benjamin Peterson0cea1572008-07-26 21:59:03 +0000136 >>> print('{1} and {0}'.format('spam', 'eggs'))
Benjamin Petersone6f00632008-05-26 01:03:56 +0000137 eggs and spam
138
Ezio Melotti2b736602011-03-13 02:19:57 +0200139If keyword arguments are used in the :meth:`str.format` method, their values
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000140are referred to by using the name of the argument. ::
Benjamin Petersone6f00632008-05-26 01:03:56 +0000141
Benjamin Peterson71141932008-07-26 22:27:04 +0000142 >>> print('This {food} is {adjective}.'.format(
143 ... food='spam', adjective='absolutely horrible'))
Benjamin Petersone6f00632008-05-26 01:03:56 +0000144 This spam is absolutely horrible.
145
146Positional and keyword arguments can be arbitrarily combined::
147
Benjamin Peterson71141932008-07-26 22:27:04 +0000148 >>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
149 other='Georg'))
Benjamin Petersone6f00632008-05-26 01:03:56 +0000150 The story of Bill, Manfred, and Georg.
151
Georg Brandl2f3ed682009-09-01 07:42:40 +0000152``'!a'`` (apply :func:`ascii`), ``'!s'`` (apply :func:`str`) and ``'!r'``
153(apply :func:`repr`) can be used to convert the value before it is formatted::
154
155 >>> import math
156 >>> print('The value of PI is approximately {}.'.format(math.pi))
157 The value of PI is approximately 3.14159265359.
158 >>> print('The value of PI is approximately {!r}.'.format(math.pi))
159 The value of PI is approximately 3.141592653589793.
160
Alexandre Vassalottie223eb82009-07-29 20:12:15 +0000161An optional ``':'`` and format specifier can follow the field name. This allows
Benjamin Petersone6f00632008-05-26 01:03:56 +0000162greater control over how the value is formatted. The following example
Raymond Hettinger756fe262011-02-24 00:06:16 +0000163rounds Pi to three places after the decimal.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165 >>> import math
Benjamin Petersone6f00632008-05-26 01:03:56 +0000166 >>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
Georg Brandl116aa622007-08-15 14:28:22 +0000167 The value of PI is approximately 3.142.
168
Benjamin Petersone6f00632008-05-26 01:03:56 +0000169Passing an integer after the ``':'`` will cause that field to be a minimum
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000170number of characters wide. This is useful for making tables pretty. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
173 >>> for name, phone in table.items():
Benjamin Petersone6f00632008-05-26 01:03:56 +0000174 ... print('{0:10} ==> {1:10d}'.format(name, phone))
Georg Brandl48310cd2009-01-03 21:18:54 +0000175 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000176 Jack ==> 4098
177 Dcab ==> 7678
178 Sjoerd ==> 4127
179
Georg Brandl116aa622007-08-15 14:28:22 +0000180If you have a really long format string that you don't want to split up, it
181would be nice if you could reference the variables to be formatted by name
Benjamin Petersone6f00632008-05-26 01:03:56 +0000182instead of by position. This can be done by simply passing the dict and using
183square brackets ``'[]'`` to access the keys ::
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
Benjamin Peterson71141932008-07-26 22:27:04 +0000186 >>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
Andrew Svetlove9cf97c2012-10-17 16:41:28 +0300187 ... 'Dcab: {0[Dcab]:d}'.format(table))
Benjamin Petersone6f00632008-05-26 01:03:56 +0000188 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
189
190This could also be done by passing the table as keyword arguments with the '**'
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000191notation. ::
Benjamin Petersone6f00632008-05-26 01:03:56 +0000192
193 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
194 >>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Georg Brandl116aa622007-08-15 14:28:22 +0000195 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
196
Ezio Melotti2b736602011-03-13 02:19:57 +0200197This is particularly useful in combination with the built-in function
198:func:`vars`, which returns a dictionary containing all local variables.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Mark Dickinson934896d2009-02-21 20:59:32 +0000200For a complete overview of string formatting with :meth:`str.format`, see
Benjamin Petersone6f00632008-05-26 01:03:56 +0000201:ref:`formatstrings`.
202
203
204Old string formatting
205---------------------
206
207The ``%`` operator can also be used for string formatting. It interprets the
Georg Brandl60203b42010-10-06 10:11:56 +0000208left argument much like a :c:func:`sprintf`\ -style format string to be applied
Benjamin Petersone6f00632008-05-26 01:03:56 +0000209to the right argument, and returns the string resulting from this formatting
210operation. For example::
211
212 >>> import math
Georg Brandl11e18b02008-08-05 09:04:16 +0000213 >>> print('The value of PI is approximately %5.3f.' % math.pi)
Benjamin Petersone6f00632008-05-26 01:03:56 +0000214 The value of PI is approximately 3.142.
215
Benjamin Petersone6f00632008-05-26 01:03:56 +0000216More information can be found in the :ref:`old-string-formatting` section.
217
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219.. _tut-files:
220
221Reading and Writing Files
222=========================
223
224.. index::
225 builtin: open
226 object: file
227
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000228:func:`open` returns a :term:`file object`, and is most commonly used with
229two arguments: ``open(filename, mode)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000230
Georg Brandl116aa622007-08-15 14:28:22 +0000231::
232
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100233 >>> f = open('workfile', 'w')
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000234
235.. XXX str(f) is <io.TextIOWrapper object at 0x82e8dc4>
236
Guido van Rossum0616b792007-08-31 03:25:11 +0000237 >>> print(f)
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100238 <open file 'workfile', mode 'w' at 80a0960>
Georg Brandl116aa622007-08-15 14:28:22 +0000239
240The first argument is a string containing the filename. The second argument is
241another string containing a few characters describing the way in which the file
242will be used. *mode* can be ``'r'`` when the file will only be read, ``'w'``
243for only writing (an existing file with the same name will be erased), and
244``'a'`` opens the file for appending; any data written to the file is
245automatically added to the end. ``'r+'`` opens the file for both reading and
246writing. The *mode* argument is optional; ``'r'`` will be assumed if it's
247omitted.
248
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000249Normally, files are opened in :dfn:`text mode`, that means, you read and write
Alessandro Cuccid8de44b2015-07-28 21:00:10 +0200250strings from and to the file, which are encoded in a specific encoding. If
Jason R. Coombs842c0742015-07-29 14:04:36 -0400251encoding is not specified, the default is platform dependent (see
252:func:`open`). ``'b'`` appended to the mode opens the file in
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000253:dfn:`binary mode`: now the data is read and written in the form of bytes
254objects. This mode should be used for all files that don't contain text.
Skip Montanaro4e02c502007-09-26 01:10:12 +0000255
Chris Jerdonek5bf7f1f2012-10-17 20:17:41 -0700256In text mode, the default when reading is to convert platform-specific line
257endings (``\n`` on Unix, ``\r\n`` on Windows) to just ``\n``. When writing in
258text mode, the default is to convert occurrences of ``\n`` back to
259platform-specific line endings. This behind-the-scenes modification
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000260to file data is fine for text files, but will corrupt binary data like that in
261:file:`JPEG` or :file:`EXE` files. Be very careful to use binary mode when
262reading and writing such files.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
265.. _tut-filemethods:
266
267Methods of File Objects
268-----------------------
269
270The rest of the examples in this section will assume that a file object called
271``f`` has already been created.
272
273To read a file's contents, call ``f.read(size)``, which reads some quantity of
Ezio Melotti397bb242016-01-12 11:27:30 +0200274data and returns it as a string (in text mode) or bytes object (in binary mode).
275*size* is an optional numeric argument. When *size* is omitted or negative, the
276entire contents of the file will be read and returned; it's your problem if the
277file is twice as large as your machine's memory. Otherwise, at most *size* bytes
278are read and returned.
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000279If the end of the file has been reached, ``f.read()`` will return an empty
280string (``''``). ::
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282 >>> f.read()
283 'This is the entire file.\n'
284 >>> f.read()
285 ''
286
287``f.readline()`` reads a single line from the file; a newline character (``\n``)
288is left at the end of the string, and is only omitted on the last line of the
289file if the file doesn't end in a newline. This makes the return value
290unambiguous; if ``f.readline()`` returns an empty string, the end of the file
291has been reached, while a blank line is represented by ``'\n'``, a string
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000292containing only a single newline. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294 >>> f.readline()
295 'This is the first line of the file.\n'
296 >>> f.readline()
297 'Second line of the file\n'
298 >>> f.readline()
299 ''
300
Ezio Melottied3cd7e2013-04-15 19:08:31 +0300301For reading lines from a file, you can loop over the file object. This is memory
302efficient, fast, and leads to simple code::
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304 >>> for line in f:
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000305 ... print(line, end='')
306 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000307 This is the first line of the file.
308 Second line of the file
309
Ezio Melottied3cd7e2013-04-15 19:08:31 +0300310If you want to read all the lines of a file in a list you can also use
311``list(f)`` or ``f.readlines()``.
Georg Brandl116aa622007-08-15 14:28:22 +0000312
313``f.write(string)`` writes the contents of *string* to the file, returning
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000314the number of characters written. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316 >>> f.write('This is a test\n')
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000317 15
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Ezio Melotti397bb242016-01-12 11:27:30 +0200319Other types of objects need to be converted -- either to a string (in text mode)
320or a bytes object (in binary mode) -- before writing them::
Georg Brandl116aa622007-08-15 14:28:22 +0000321
322 >>> value = ('the answer', 42)
Ezio Melotti397bb242016-01-12 11:27:30 +0200323 >>> s = str(value) # convert the tuple to string
Georg Brandl116aa622007-08-15 14:28:22 +0000324 >>> f.write(s)
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000325 18
Georg Brandl116aa622007-08-15 14:28:22 +0000326
R David Murray1c4e4432013-07-30 15:51:57 -0400327``f.tell()`` returns an integer giving the file object's current position in the file
Georg Brandl6b4c8472014-10-30 22:26:26 +0100328represented as number of bytes from the beginning of the file when in binary mode and
329an opaque number when in text mode.
R David Murray1c4e4432013-07-30 15:51:57 -0400330
331To change the file object's position, use ``f.seek(offset, from_what)``. The position is computed
Georg Brandl116aa622007-08-15 14:28:22 +0000332from adding *offset* to a reference point; the reference point is selected by
333the *from_what* argument. A *from_what* value of 0 measures from the beginning
334of the file, 1 uses the current file position, and 2 uses the end of the file as
335the reference point. *from_what* can be omitted and defaults to 0, using the
336beginning of the file as the reference point. ::
337
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100338 >>> f = open('workfile', 'rb+')
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000339 >>> f.write(b'0123456789abcdef')
340 16
Georg Brandl116aa622007-08-15 14:28:22 +0000341 >>> f.seek(5) # Go to the 6th byte in the file
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000342 5
Georg Brandl48310cd2009-01-03 21:18:54 +0000343 >>> f.read(1)
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000344 b'5'
Georg Brandl116aa622007-08-15 14:28:22 +0000345 >>> f.seek(-3, 2) # Go to the 3rd byte before the end
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000346 13
Georg Brandl116aa622007-08-15 14:28:22 +0000347 >>> f.read(1)
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000348 b'd'
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Georg Brandl0dcb7ac2008-08-08 07:04:38 +0000350In text files (those opened without a ``b`` in the mode string), only seeks
351relative to the beginning of the file are allowed (the exception being seeking
R David Murray1c4e4432013-07-30 15:51:57 -0400352to the very file end with ``seek(0, 2)``) and the only valid *offset* values are
353those returned from the ``f.tell()``, or zero. Any other *offset* value produces
354undefined behaviour.
355
Georg Brandl48310cd2009-01-03 21:18:54 +0000356
Georg Brandl116aa622007-08-15 14:28:22 +0000357When you're done with a file, call ``f.close()`` to close it and free up any
358system resources taken up by the open file. After calling ``f.close()``,
359attempts to use the file object will automatically fail. ::
360
361 >>> f.close()
362 >>> f.read()
363 Traceback (most recent call last):
364 File "<stdin>", line 1, in ?
365 ValueError: I/O operation on closed file
366
Georg Brandl3dbca812008-07-23 16:10:53 +0000367It is good practice to use the :keyword:`with` keyword when dealing with file
368objects. This has the advantage that the file is properly closed after its
369suite finishes, even if an exception is raised on the way. It is also much
370shorter than writing equivalent :keyword:`try`\ -\ :keyword:`finally` blocks::
371
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100372 >>> with open('workfile', 'r') as f:
Georg Brandl3dbca812008-07-23 16:10:53 +0000373 ... read_data = f.read()
374 >>> f.closed
375 True
376
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000377File objects have some additional methods, such as :meth:`~file.isatty` and
378:meth:`~file.truncate` which are less frequently used; consult the Library
379Reference for a complete guide to file objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381
Antoine Pitroudd799d22013-12-05 23:46:32 +0100382.. _tut-json:
Georg Brandl116aa622007-08-15 14:28:22 +0000383
Antoine Pitroudd799d22013-12-05 23:46:32 +0100384Saving structured data with :mod:`json`
385---------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Antoine Pitroudd799d22013-12-05 23:46:32 +0100387.. index:: module: json
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Antoine Pitroudd799d22013-12-05 23:46:32 +0100389Strings can easily be written to and read from a file. Numbers take a bit more
Georg Brandl116aa622007-08-15 14:28:22 +0000390effort, since the :meth:`read` method only returns strings, which will have to
391be passed to a function like :func:`int`, which takes a string like ``'123'``
Antoine Pitroudd799d22013-12-05 23:46:32 +0100392and returns its numeric value 123. When you want to save more complex data
393types like nested lists and dictionaries, parsing and serializing by hand
394becomes complicated.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Antoine Pitroudd799d22013-12-05 23:46:32 +0100396Rather than having users constantly writing and debugging code to save
397complicated data types to files, Python allows you to use the popular data
398interchange format called `JSON (JavaScript Object Notation)
399<http://json.org>`_. The standard module called :mod:`json` can take Python
400data hierarchies, and convert them to string representations; this process is
401called :dfn:`serializing`. Reconstructing the data from the string representation
402is called :dfn:`deserializing`. Between serializing and deserializing, the
403string representing the object may have been stored in a file or data, or
Georg Brandl116aa622007-08-15 14:28:22 +0000404sent over a network connection to some distant machine.
405
Antoine Pitroudd799d22013-12-05 23:46:32 +0100406.. note::
407 The JSON format is commonly used by modern applications to allow for data
408 exchange. Many programmers are already familiar with it, which makes
409 it a good choice for interoperability.
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Antoine Pitroudd799d22013-12-05 23:46:32 +0100411If you have an object ``x``, you can view its JSON string representation with a
412simple line of code::
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Antoine Pitroudd799d22013-12-05 23:46:32 +0100414 >>> json.dumps([1, 'simple', 'list'])
415 '[1, "simple", "list"]'
Georg Brandl116aa622007-08-15 14:28:22 +0000416
Antoine Pitroudd799d22013-12-05 23:46:32 +0100417Another variant of the :func:`~json.dumps` function, called :func:`~json.dump`,
418simply serializes the object to a :term:`text file`. So if ``f`` is a
419:term:`text file` object opened for writing, we can do this::
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Antoine Pitroudd799d22013-12-05 23:46:32 +0100421 json.dump(x, f)
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Antoine Pitroudd799d22013-12-05 23:46:32 +0100423To decode the object again, if ``f`` is a :term:`text file` object which has
424been opened for reading::
Georg Brandl116aa622007-08-15 14:28:22 +0000425
Antoine Pitroudd799d22013-12-05 23:46:32 +0100426 x = json.load(f)
427
428This simple serialization technique can handle lists and dictionaries, but
429serializing arbitrary class instances in JSON requires a bit of extra effort.
430The reference for the :mod:`json` module contains an explanation of this.
431
432.. seealso::
433
434 :mod:`pickle` - the pickle module
435
436 Contrary to :ref:`JSON <tut-json>`, *pickle* is a protocol which allows
437 the serialization of arbitrarily complex Python objects. As such, it is
438 specific to Python and cannot be used to communicate with applications
439 written in other languages. It is also insecure by default:
440 deserializing pickle data coming from an untrusted source can execute
441 arbitrary code, if the data was crafted by a skilled attacker.