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