~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Doc/tutorial/inputoutput.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
127
127
   We are the knights who say "Ni!"
128
128
 
129
129
The brackets and characters within them (called format fields) are replaced with
130
 
the objects passed into the format method.  The number in the brackets refers to
131
 
the position of the object passed into the format method. ::
 
130
the objects passed into the :meth:`~str.format` method.  A number in the
 
131
brackets refers to the position of the object passed into the
 
132
:meth:`~str.format` method. ::
132
133
 
133
134
   >>> print '{0} and {1}'.format('spam', 'eggs')
134
135
   spam and eggs
135
136
   >>> print '{1} and {0}'.format('spam', 'eggs')
136
137
   eggs and spam
137
138
 
138
 
If keyword arguments are used in the format method, their values are referred to
139
 
by using the name of the argument. ::
 
139
If keyword arguments are used in the :meth:`~str.format` method, their values
 
140
are referred to by using the name of the argument. ::
140
141
 
141
142
   >>> print 'This {food} is {adjective}.'.format(
142
143
   ...       food='spam', adjective='absolutely horrible')
148
149
   ...                                                    other='Georg')
149
150
   The story of Bill, Manfred, and Georg.
150
151
 
151
 
An optional ``':'`` and format specifier can follow the field name. This also
 
152
``'!s'`` (apply :func:`str`) and ``'!r'`` (apply :func:`repr`) can be used to
 
153
convert the value before it is formatted. ::
 
154
 
 
155
   >>> import math
 
156
   >>> print 'The value of PI is approximately {0}.'.format(math.pi)
 
157
   The value of PI is approximately 3.14159265359.
 
158
   >>> print 'The value of PI is approximately {0!r}.'.format(math.pi)
 
159
   The value of PI is approximately 3.141592653589793.
 
160
 
 
161
An optional ``':'`` and format specifier can follow the field name. This allows
152
162
greater control over how the value is formatted.  The following example
153
 
truncates the Pi to three places after the decimal.
 
163
truncates Pi to three places after the decimal.
154
164
 
155
165
   >>> import math
156
166
   >>> print 'The value of PI is approximately {0:.3f}.'.format(math.pi)
157
167
   The value of PI is approximately 3.142.
158
168
 
159
169
Passing an integer after the ``':'`` will cause that field to be a minimum
160
 
number of characters wide.  This is useful for making tables pretty.::
 
170
number of characters wide.  This is useful for making tables pretty. ::
161
171
 
162
172
   >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
163
173
   >>> for name, phone in table.items():
178
188
   Jack: 4098; Sjoerd: 4127; Dcab: 8637678
179
189
 
180
190
This could also be done by passing the table as keyword arguments with the '**'
181
 
notation.::
 
191
notation. ::
182
192
 
183
193
   >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
184
194
   >>> print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)
204
214
   The value of PI is approximately 3.142.
205
215
 
206
216
Since :meth:`str.format` is quite new, a lot of Python code still uses the ``%``
207
 
operator. However, because this old style of formatting will eventually removed
208
 
from the language :meth:`str.format` should generally be used.
 
217
operator. However, because this old style of formatting will eventually be
 
218
removed from the language, :meth:`str.format` should generally be used.
209
219
 
210
220
More information can be found in the :ref:`string-formatting` section.
211
221
 
238
248
omitted.
239
249
 
240
250
On Windows, ``'b'`` appended to the mode opens the file in binary mode, so there
241
 
are also modes like ``'rb'``, ``'wb'``, and ``'r+b'``.  Windows makes a
242
 
distinction between text and binary files; the end-of-line characters in text
 
251
are also modes like ``'rb'``, ``'wb'``, and ``'r+b'``.  Python on Windows makes
 
252
a distinction between text and binary files; the end-of-line characters in text
243
253
files are automatically altered slightly when data is read or written.  This
244
254
behind-the-scenes modification to file data is fine for ASCII text files, but
245
255
it'll corrupt binary data like that in :file:`JPEG` or :file:`EXE` files.  Be
356
366
    >>> f.closed
357
367
    True
358
368
 
359
 
File objects have some additional methods, such as :meth:`isatty` and
360
 
:meth:`truncate` which are less frequently used; consult the Library Reference
361
 
for a complete guide to file objects.
 
369
File objects have some additional methods, such as :meth:`~file.isatty` and
 
370
:meth:`~file.truncate` which are less frequently used; consult the Library
 
371
Reference for a complete guide to file objects.
362
372
 
363
373
 
364
374
.. _tut-pickle: