2007-08-15 11:28:01 -03:00
|
|
|
.. _tut-io:
|
|
|
|
|
|
|
|
****************
|
|
|
|
Input and Output
|
|
|
|
****************
|
|
|
|
|
|
|
|
There are several ways to present the output of a program; data can be printed
|
|
|
|
in a human-readable form, or written to a file for future use. This chapter will
|
|
|
|
discuss some of the possibilities.
|
|
|
|
|
|
|
|
|
|
|
|
.. _tut-formatting:
|
|
|
|
|
|
|
|
Fancier Output Formatting
|
|
|
|
=========================
|
|
|
|
|
|
|
|
So far we've encountered two ways of writing values: *expression statements* and
|
|
|
|
the :keyword:`print` statement. (A third way is using the :meth:`write` method
|
|
|
|
of file objects; the standard output file can be referenced as ``sys.stdout``.
|
|
|
|
See the Library Reference for more information on this.)
|
|
|
|
|
|
|
|
.. index:: module: string
|
|
|
|
|
|
|
|
Often you'll want more control over the formatting of your output than simply
|
|
|
|
printing space-separated values. There are two ways to format your output; the
|
|
|
|
first way is to do all the string handling yourself; using string slicing and
|
|
|
|
concatenation operations you can create any layout you can imagine. The
|
|
|
|
standard module :mod:`string` contains some useful operations for padding
|
|
|
|
strings to a given column width; these will be discussed shortly. The second
|
2008-05-25 21:54:22 -03:00
|
|
|
way is to use the :meth:`str.format` method.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
One question remains, of course: how do you convert values to strings? Luckily,
|
|
|
|
Python has ways to convert any value to a string: pass it to the :func:`repr`
|
2008-08-08 12:34:34 -03:00
|
|
|
or :func:`str` functions.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
The :func:`str` function is meant to return representations of values which are
|
|
|
|
fairly human-readable, while :func:`repr` is meant to generate representations
|
|
|
|
which can be read by the interpreter (or will force a :exc:`SyntaxError` if
|
|
|
|
there is not equivalent syntax). For objects which don't have a particular
|
|
|
|
representation for human consumption, :func:`str` will return the same value as
|
|
|
|
:func:`repr`. Many values, such as numbers or structures like lists and
|
|
|
|
dictionaries, have the same representation using either function. Strings and
|
|
|
|
floating point numbers, in particular, have two distinct representations.
|
|
|
|
|
|
|
|
Some examples::
|
|
|
|
|
|
|
|
>>> s = 'Hello, world.'
|
|
|
|
>>> str(s)
|
|
|
|
'Hello, world.'
|
|
|
|
>>> repr(s)
|
|
|
|
"'Hello, world.'"
|
|
|
|
>>> str(0.1)
|
|
|
|
'0.1'
|
|
|
|
>>> repr(0.1)
|
|
|
|
'0.10000000000000001'
|
|
|
|
>>> x = 10 * 3.25
|
|
|
|
>>> y = 200 * 200
|
|
|
|
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
|
|
|
|
>>> print s
|
|
|
|
The value of x is 32.5, and y is 40000...
|
|
|
|
>>> # The repr() of a string adds string quotes and backslashes:
|
|
|
|
... hello = 'hello, world\n'
|
|
|
|
>>> hellos = repr(hello)
|
|
|
|
>>> print hellos
|
|
|
|
'hello, world\n'
|
|
|
|
>>> # The argument to repr() may be any Python object:
|
|
|
|
... repr((x, y, ('spam', 'eggs')))
|
|
|
|
"(32.5, 40000, ('spam', 'eggs'))"
|
|
|
|
|
|
|
|
Here are two ways to write a table of squares and cubes::
|
|
|
|
|
|
|
|
>>> for x in range(1, 11):
|
|
|
|
... print repr(x).rjust(2), repr(x*x).rjust(3),
|
|
|
|
... # Note trailing comma on previous line
|
|
|
|
... print repr(x*x*x).rjust(4)
|
|
|
|
...
|
|
|
|
1 1 1
|
|
|
|
2 4 8
|
|
|
|
3 9 27
|
|
|
|
4 16 64
|
|
|
|
5 25 125
|
|
|
|
6 36 216
|
|
|
|
7 49 343
|
|
|
|
8 64 512
|
|
|
|
9 81 729
|
|
|
|
10 100 1000
|
|
|
|
|
|
|
|
>>> for x in range(1,11):
|
2008-05-25 21:54:22 -03:00
|
|
|
... print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
...
|
2007-08-15 11:28:01 -03:00
|
|
|
1 1 1
|
|
|
|
2 4 8
|
|
|
|
3 9 27
|
|
|
|
4 16 64
|
|
|
|
5 25 125
|
|
|
|
6 36 216
|
|
|
|
7 49 343
|
|
|
|
8 64 512
|
|
|
|
9 81 729
|
|
|
|
10 100 1000
|
|
|
|
|
|
|
|
(Note that in the first example, one space between each column was added by the
|
|
|
|
way :keyword:`print` works: it always adds spaces between its arguments.)
|
|
|
|
|
|
|
|
This example demonstrates the :meth:`rjust` method of string objects, which
|
|
|
|
right-justifies a string in a field of a given width by padding it with spaces
|
|
|
|
on the left. There are similar methods :meth:`ljust` and :meth:`center`. These
|
|
|
|
methods do not write anything, they just return a new string. If the input
|
|
|
|
string is too long, they don't truncate it, but return it unchanged; this will
|
|
|
|
mess up your column lay-out but that's usually better than the alternative,
|
|
|
|
which would be lying about a value. (If you really want truncation you can
|
|
|
|
always add a slice operation, as in ``x.ljust(n)[:n]``.)
|
|
|
|
|
|
|
|
There is another method, :meth:`zfill`, which pads a numeric string on the left
|
|
|
|
with zeros. It understands about plus and minus signs::
|
|
|
|
|
|
|
|
>>> '12'.zfill(5)
|
|
|
|
'00012'
|
|
|
|
>>> '-3.14'.zfill(7)
|
|
|
|
'-003.14'
|
|
|
|
>>> '3.14159265359'.zfill(5)
|
|
|
|
'3.14159265359'
|
|
|
|
|
2008-05-25 21:54:22 -03:00
|
|
|
Basic usage of the :meth:`str.format` method looks like this::
|
|
|
|
|
2010-01-05 20:55:21 -04:00
|
|
|
>>> print 'We are the {0} who say "{1}!"'.format('knights', 'Ni')
|
2008-05-25 21:54:22 -03:00
|
|
|
We are the knights who say "Ni!"
|
|
|
|
|
|
|
|
The brackets and characters within them (called format fields) are replaced with
|
Merged revisions 74492,74531,74545-74550,74553-74555,74588,74603,74608,74614,74616-74618,74631-74633,74652-74653,74666,74671,74737,74739,74779,74781-74782,74784,74791,74793,74818-74820,74822,74832 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74492 | r.david.murray | 2009-08-17 21:26:49 +0200 (Mo, 17 Aug 2009) | 2 lines
Issue 6685: 'toupper' -> 'upper' in cgi doc example explanation.
........
r74531 | vinay.sajip | 2009-08-21 00:04:32 +0200 (Fr, 21 Aug 2009) | 1 line
Added section on exceptions raised during logging.
........
r74545 | georg.brandl | 2009-08-24 19:14:29 +0200 (Mo, 24 Aug 2009) | 1 line
#6772: mention utf-8 as utf8 alias.
........
r74546 | georg.brandl | 2009-08-24 19:20:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6725: spell "namespace" consistently.
........
r74547 | georg.brandl | 2009-08-24 19:22:05 +0200 (Mo, 24 Aug 2009) | 1 line
#6718: fix example.
........
r74548 | georg.brandl | 2009-08-24 19:24:27 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: mention "deleting" as an alias for removing files.
........
r74549 | benjamin.peterson | 2009-08-24 19:42:36 +0200 (Mo, 24 Aug 2009) | 1 line
fix pdf building by teaching latex the right encoding package
........
r74550 | georg.brandl | 2009-08-24 19:48:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: note that rmdir only removes empty directories.
........
r74553 | r.david.murray | 2009-08-27 03:04:59 +0200 (Do, 27 Aug 2009) | 2 lines
Remove leftover text from end of sentence.
........
r74554 | georg.brandl | 2009-08-27 20:59:02 +0200 (Do, 27 Aug 2009) | 1 line
Typo fix.
........
r74555 | georg.brandl | 2009-08-27 21:02:43 +0200 (Do, 27 Aug 2009) | 1 line
#6787: reference fix.
........
r74588 | georg.brandl | 2009-08-30 10:35:01 +0200 (So, 30 Aug 2009) | 1 line
#6803: fix old name.
........
r74603 | georg.brandl | 2009-08-31 08:38:29 +0200 (Mo, 31 Aug 2009) | 1 line
other -> others where multiple arguments are accepted.
........
r74608 | senthil.kumaran | 2009-08-31 18:40:27 +0200 (Mo, 31 Aug 2009) | 3 lines
Doc fix for the issue2637.
........
r74614 | georg.brandl | 2009-09-01 09:40:54 +0200 (Di, 01 Sep 2009) | 1 line
#6813: better documentation for numberless string formats.
........
r74616 | georg.brandl | 2009-09-01 09:46:26 +0200 (Di, 01 Sep 2009) | 1 line
#6808: clarification.
........
r74617 | georg.brandl | 2009-09-01 09:53:37 +0200 (Di, 01 Sep 2009) | 1 line
#6765: hint that log(x, base) is not very sophisticated.
........
r74618 | georg.brandl | 2009-09-01 10:00:47 +0200 (Di, 01 Sep 2009) | 1 line
#6810: add a link to the section about frame objects instead of just a description where to find it.
........
r74631 | georg.brandl | 2009-09-02 22:37:16 +0200 (Mi, 02 Sep 2009) | 1 line
#6821: fix signature of PyBuffer_Release().
........
r74632 | georg.brandl | 2009-09-03 09:27:26 +0200 (Do, 03 Sep 2009) | 1 line
#6828: fix wrongly highlighted blocks.
........
r74633 | georg.brandl | 2009-09-03 14:31:39 +0200 (Do, 03 Sep 2009) | 1 line
#6757: complete the list of types that marshal can serialize.
........
r74652 | georg.brandl | 2009-09-04 13:25:37 +0200 (Fr, 04 Sep 2009) | 1 line
#6756: add some info about the "acct" parameter.
........
r74653 | georg.brandl | 2009-09-04 13:32:18 +0200 (Fr, 04 Sep 2009) | 1 line
#6777: dont discourage usage of Exception.args or promote usage of Exception.message.
........
r74666 | georg.brandl | 2009-09-05 11:04:09 +0200 (Sa, 05 Sep 2009) | 1 line
#6841: remove duplicated word.
........
r74671 | georg.brandl | 2009-09-05 18:47:17 +0200 (Sa, 05 Sep 2009) | 1 line
#6843: add link from filterwarnings to where the meaning of the arguments is covered.
........
r74737 | georg.brandl | 2009-09-09 18:49:13 +0200 (Mi, 09 Sep 2009) | 1 line
Properly document copy and deepcopy as functions.
........
r74739 | georg.brandl | 2009-09-11 09:55:20 +0200 (Fr, 11 Sep 2009) | 1 line
Move function back to its section.
........
r74779 | michael.foord | 2009-09-13 18:13:36 +0200 (So, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74781 | michael.foord | 2009-09-13 18:46:19 +0200 (So, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 19:07:46 +0200 (So, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74784 | georg.brandl | 2009-09-13 20:15:07 +0200 (So, 13 Sep 2009) | 1 line
Typo fix.
........
r74791 | georg.brandl | 2009-09-14 16:08:54 +0200 (Mo, 14 Sep 2009) | 1 line
#6574: list the future features in a table.
........
r74793 | georg.brandl | 2009-09-14 16:50:47 +0200 (Mo, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line
#6880: add reference to classes section in exceptions section, which comes earlier.
........
r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line
#6876: fix base class constructor invocation in example.
........
r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line
#6891: comment out dead link to Unicode article.
........
r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line
#5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign.
........
r74832 | georg.brandl | 2009-09-16 17:57:46 +0200 (Mi, 16 Sep 2009) | 1 line
Rewrap long lines.
........
2009-10-27 11:50:20 -03:00
|
|
|
the objects passed into the :meth:`~str.format` method. A number in the
|
Merged revisions 74210,74239,74252-74253,74256,74258-74261,74332-74333,74404,74411,74445,74465,74467,74488 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74210 | georg.brandl | 2009-07-26 16:44:23 +0200 (So, 26 Jul 2009) | 1 line
Move member descriptions inside the classes.
........
r74239 | georg.brandl | 2009-07-28 20:55:32 +0200 (Di, 28 Jul 2009) | 1 line
Clarify quote_plus() usage.
........
r74252 | georg.brandl | 2009-07-29 18:06:31 +0200 (Mi, 29 Jul 2009) | 1 line
#6593: fix link targets.
........
r74253 | georg.brandl | 2009-07-29 18:09:17 +0200 (Mi, 29 Jul 2009) | 1 line
#6591: add reference to ioctl in fcntl module for platforms other than Windows.
........
r74256 | georg.brandl | 2009-07-29 18:32:30 +0200 (Mi, 29 Jul 2009) | 1 line
#6336: Add nb_divide.
........
r74258 | georg.brandl | 2009-07-29 18:57:05 +0200 (Mi, 29 Jul 2009) | 1 line
Add a link to readline, and mention IPython and bpython.
........
r74259 | georg.brandl | 2009-07-29 19:07:21 +0200 (Mi, 29 Jul 2009) | 1 line
Fix some markup and small factual glitches found by M. Markert.
........
r74260 | georg.brandl | 2009-07-29 19:15:20 +0200 (Mi, 29 Jul 2009) | 1 line
Fix a few markup glitches.
........
r74261 | georg.brandl | 2009-07-29 19:50:25 +0200 (Mi, 29 Jul 2009) | 1 line
Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better.
........
r74332 | georg.brandl | 2009-08-06 19:23:21 +0200 (Do, 06 Aug 2009) | 1 line
Fix punctuation and one copy-paste error.
........
r74333 | georg.brandl | 2009-08-06 19:43:55 +0200 (Do, 06 Aug 2009) | 1 line
#6658: fix two typos.
........
r74404 | georg.brandl | 2009-08-13 14:05:52 +0200 (Do, 13 Aug 2009) | 1 line
Use locale.format_string() for more than one specifier.
........
r74411 | georg.brandl | 2009-08-13 14:57:25 +0200 (Do, 13 Aug 2009) | 2 lines
Remove potentially confusing sentence in __mangling description.
........
r74445 | vinay.sajip | 2009-08-14 13:33:54 +0200 (Fr, 14 Aug 2009) | 1 line
Added versionchanged notices for optional 'delay' parameter to file handler classes.
........
r74465 | vinay.sajip | 2009-08-16 01:23:12 +0200 (So, 16 Aug 2009) | 1 line
Added section on logging to one file from multiple processes.
........
r74467 | vinay.sajip | 2009-08-16 01:34:47 +0200 (So, 16 Aug 2009) | 1 line
Refined section on logging to one file from multiple processes.
........
r74488 | vinay.sajip | 2009-08-17 15:14:37 +0200 (Mo, 17 Aug 2009) | 1 line
Further refined section on logging to one file from multiple processes.
........
2009-10-27 11:41:50 -03:00
|
|
|
brackets refers to the position of the object passed into the
|
|
|
|
:meth:`~str.format` method. ::
|
2008-05-25 21:54:22 -03:00
|
|
|
|
|
|
|
>>> print '{0} and {1}'.format('spam', 'eggs')
|
|
|
|
spam and eggs
|
|
|
|
>>> print '{1} and {0}'.format('spam', 'eggs')
|
|
|
|
eggs and spam
|
|
|
|
|
Merged revisions 74210,74239,74252-74253,74256,74258-74261,74332-74333,74404,74411,74445,74465,74467,74488 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74210 | georg.brandl | 2009-07-26 16:44:23 +0200 (So, 26 Jul 2009) | 1 line
Move member descriptions inside the classes.
........
r74239 | georg.brandl | 2009-07-28 20:55:32 +0200 (Di, 28 Jul 2009) | 1 line
Clarify quote_plus() usage.
........
r74252 | georg.brandl | 2009-07-29 18:06:31 +0200 (Mi, 29 Jul 2009) | 1 line
#6593: fix link targets.
........
r74253 | georg.brandl | 2009-07-29 18:09:17 +0200 (Mi, 29 Jul 2009) | 1 line
#6591: add reference to ioctl in fcntl module for platforms other than Windows.
........
r74256 | georg.brandl | 2009-07-29 18:32:30 +0200 (Mi, 29 Jul 2009) | 1 line
#6336: Add nb_divide.
........
r74258 | georg.brandl | 2009-07-29 18:57:05 +0200 (Mi, 29 Jul 2009) | 1 line
Add a link to readline, and mention IPython and bpython.
........
r74259 | georg.brandl | 2009-07-29 19:07:21 +0200 (Mi, 29 Jul 2009) | 1 line
Fix some markup and small factual glitches found by M. Markert.
........
r74260 | georg.brandl | 2009-07-29 19:15:20 +0200 (Mi, 29 Jul 2009) | 1 line
Fix a few markup glitches.
........
r74261 | georg.brandl | 2009-07-29 19:50:25 +0200 (Mi, 29 Jul 2009) | 1 line
Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better.
........
r74332 | georg.brandl | 2009-08-06 19:23:21 +0200 (Do, 06 Aug 2009) | 1 line
Fix punctuation and one copy-paste error.
........
r74333 | georg.brandl | 2009-08-06 19:43:55 +0200 (Do, 06 Aug 2009) | 1 line
#6658: fix two typos.
........
r74404 | georg.brandl | 2009-08-13 14:05:52 +0200 (Do, 13 Aug 2009) | 1 line
Use locale.format_string() for more than one specifier.
........
r74411 | georg.brandl | 2009-08-13 14:57:25 +0200 (Do, 13 Aug 2009) | 2 lines
Remove potentially confusing sentence in __mangling description.
........
r74445 | vinay.sajip | 2009-08-14 13:33:54 +0200 (Fr, 14 Aug 2009) | 1 line
Added versionchanged notices for optional 'delay' parameter to file handler classes.
........
r74465 | vinay.sajip | 2009-08-16 01:23:12 +0200 (So, 16 Aug 2009) | 1 line
Added section on logging to one file from multiple processes.
........
r74467 | vinay.sajip | 2009-08-16 01:34:47 +0200 (So, 16 Aug 2009) | 1 line
Refined section on logging to one file from multiple processes.
........
r74488 | vinay.sajip | 2009-08-17 15:14:37 +0200 (Mo, 17 Aug 2009) | 1 line
Further refined section on logging to one file from multiple processes.
........
2009-10-27 11:41:50 -03:00
|
|
|
If keyword arguments are used in the :meth:`~str.format` method, their values
|
|
|
|
are referred to by using the name of the argument. ::
|
2008-05-25 21:54:22 -03:00
|
|
|
|
2008-07-26 19:13:29 -03:00
|
|
|
>>> print 'This {food} is {adjective}.'.format(
|
|
|
|
... food='spam', adjective='absolutely horrible')
|
2008-05-25 21:54:22 -03:00
|
|
|
This spam is absolutely horrible.
|
|
|
|
|
|
|
|
Positional and keyword arguments can be arbitrarily combined::
|
|
|
|
|
2008-07-26 19:13:29 -03:00
|
|
|
>>> print 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
|
|
|
|
... other='Georg')
|
2008-05-25 21:54:22 -03:00
|
|
|
The story of Bill, Manfred, and Georg.
|
|
|
|
|
Merged revisions 74492,74531,74545-74550,74553-74555,74588,74603,74608,74614,74616-74618,74631-74633,74652-74653,74666,74671,74737,74739,74779,74781-74782,74784,74791,74793,74818-74820,74822,74832 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74492 | r.david.murray | 2009-08-17 21:26:49 +0200 (Mo, 17 Aug 2009) | 2 lines
Issue 6685: 'toupper' -> 'upper' in cgi doc example explanation.
........
r74531 | vinay.sajip | 2009-08-21 00:04:32 +0200 (Fr, 21 Aug 2009) | 1 line
Added section on exceptions raised during logging.
........
r74545 | georg.brandl | 2009-08-24 19:14:29 +0200 (Mo, 24 Aug 2009) | 1 line
#6772: mention utf-8 as utf8 alias.
........
r74546 | georg.brandl | 2009-08-24 19:20:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6725: spell "namespace" consistently.
........
r74547 | georg.brandl | 2009-08-24 19:22:05 +0200 (Mo, 24 Aug 2009) | 1 line
#6718: fix example.
........
r74548 | georg.brandl | 2009-08-24 19:24:27 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: mention "deleting" as an alias for removing files.
........
r74549 | benjamin.peterson | 2009-08-24 19:42:36 +0200 (Mo, 24 Aug 2009) | 1 line
fix pdf building by teaching latex the right encoding package
........
r74550 | georg.brandl | 2009-08-24 19:48:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: note that rmdir only removes empty directories.
........
r74553 | r.david.murray | 2009-08-27 03:04:59 +0200 (Do, 27 Aug 2009) | 2 lines
Remove leftover text from end of sentence.
........
r74554 | georg.brandl | 2009-08-27 20:59:02 +0200 (Do, 27 Aug 2009) | 1 line
Typo fix.
........
r74555 | georg.brandl | 2009-08-27 21:02:43 +0200 (Do, 27 Aug 2009) | 1 line
#6787: reference fix.
........
r74588 | georg.brandl | 2009-08-30 10:35:01 +0200 (So, 30 Aug 2009) | 1 line
#6803: fix old name.
........
r74603 | georg.brandl | 2009-08-31 08:38:29 +0200 (Mo, 31 Aug 2009) | 1 line
other -> others where multiple arguments are accepted.
........
r74608 | senthil.kumaran | 2009-08-31 18:40:27 +0200 (Mo, 31 Aug 2009) | 3 lines
Doc fix for the issue2637.
........
r74614 | georg.brandl | 2009-09-01 09:40:54 +0200 (Di, 01 Sep 2009) | 1 line
#6813: better documentation for numberless string formats.
........
r74616 | georg.brandl | 2009-09-01 09:46:26 +0200 (Di, 01 Sep 2009) | 1 line
#6808: clarification.
........
r74617 | georg.brandl | 2009-09-01 09:53:37 +0200 (Di, 01 Sep 2009) | 1 line
#6765: hint that log(x, base) is not very sophisticated.
........
r74618 | georg.brandl | 2009-09-01 10:00:47 +0200 (Di, 01 Sep 2009) | 1 line
#6810: add a link to the section about frame objects instead of just a description where to find it.
........
r74631 | georg.brandl | 2009-09-02 22:37:16 +0200 (Mi, 02 Sep 2009) | 1 line
#6821: fix signature of PyBuffer_Release().
........
r74632 | georg.brandl | 2009-09-03 09:27:26 +0200 (Do, 03 Sep 2009) | 1 line
#6828: fix wrongly highlighted blocks.
........
r74633 | georg.brandl | 2009-09-03 14:31:39 +0200 (Do, 03 Sep 2009) | 1 line
#6757: complete the list of types that marshal can serialize.
........
r74652 | georg.brandl | 2009-09-04 13:25:37 +0200 (Fr, 04 Sep 2009) | 1 line
#6756: add some info about the "acct" parameter.
........
r74653 | georg.brandl | 2009-09-04 13:32:18 +0200 (Fr, 04 Sep 2009) | 1 line
#6777: dont discourage usage of Exception.args or promote usage of Exception.message.
........
r74666 | georg.brandl | 2009-09-05 11:04:09 +0200 (Sa, 05 Sep 2009) | 1 line
#6841: remove duplicated word.
........
r74671 | georg.brandl | 2009-09-05 18:47:17 +0200 (Sa, 05 Sep 2009) | 1 line
#6843: add link from filterwarnings to where the meaning of the arguments is covered.
........
r74737 | georg.brandl | 2009-09-09 18:49:13 +0200 (Mi, 09 Sep 2009) | 1 line
Properly document copy and deepcopy as functions.
........
r74739 | georg.brandl | 2009-09-11 09:55:20 +0200 (Fr, 11 Sep 2009) | 1 line
Move function back to its section.
........
r74779 | michael.foord | 2009-09-13 18:13:36 +0200 (So, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74781 | michael.foord | 2009-09-13 18:46:19 +0200 (So, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 19:07:46 +0200 (So, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74784 | georg.brandl | 2009-09-13 20:15:07 +0200 (So, 13 Sep 2009) | 1 line
Typo fix.
........
r74791 | georg.brandl | 2009-09-14 16:08:54 +0200 (Mo, 14 Sep 2009) | 1 line
#6574: list the future features in a table.
........
r74793 | georg.brandl | 2009-09-14 16:50:47 +0200 (Mo, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line
#6880: add reference to classes section in exceptions section, which comes earlier.
........
r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line
#6876: fix base class constructor invocation in example.
........
r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line
#6891: comment out dead link to Unicode article.
........
r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line
#5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign.
........
r74832 | georg.brandl | 2009-09-16 17:57:46 +0200 (Mi, 16 Sep 2009) | 1 line
Rewrap long lines.
........
2009-10-27 11:50:20 -03:00
|
|
|
``'!s'`` (apply :func:`str`) and ``'!r'`` (apply :func:`repr`) can be used to
|
|
|
|
convert the value before it is formatted. ::
|
|
|
|
|
|
|
|
>>> import math
|
2010-01-05 20:55:21 -04:00
|
|
|
>>> print 'The value of PI is approximately {0}.'.format(math.pi)
|
Merged revisions 74492,74531,74545-74550,74553-74555,74588,74603,74608,74614,74616-74618,74631-74633,74652-74653,74666,74671,74737,74739,74779,74781-74782,74784,74791,74793,74818-74820,74822,74832 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74492 | r.david.murray | 2009-08-17 21:26:49 +0200 (Mo, 17 Aug 2009) | 2 lines
Issue 6685: 'toupper' -> 'upper' in cgi doc example explanation.
........
r74531 | vinay.sajip | 2009-08-21 00:04:32 +0200 (Fr, 21 Aug 2009) | 1 line
Added section on exceptions raised during logging.
........
r74545 | georg.brandl | 2009-08-24 19:14:29 +0200 (Mo, 24 Aug 2009) | 1 line
#6772: mention utf-8 as utf8 alias.
........
r74546 | georg.brandl | 2009-08-24 19:20:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6725: spell "namespace" consistently.
........
r74547 | georg.brandl | 2009-08-24 19:22:05 +0200 (Mo, 24 Aug 2009) | 1 line
#6718: fix example.
........
r74548 | georg.brandl | 2009-08-24 19:24:27 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: mention "deleting" as an alias for removing files.
........
r74549 | benjamin.peterson | 2009-08-24 19:42:36 +0200 (Mo, 24 Aug 2009) | 1 line
fix pdf building by teaching latex the right encoding package
........
r74550 | georg.brandl | 2009-08-24 19:48:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: note that rmdir only removes empty directories.
........
r74553 | r.david.murray | 2009-08-27 03:04:59 +0200 (Do, 27 Aug 2009) | 2 lines
Remove leftover text from end of sentence.
........
r74554 | georg.brandl | 2009-08-27 20:59:02 +0200 (Do, 27 Aug 2009) | 1 line
Typo fix.
........
r74555 | georg.brandl | 2009-08-27 21:02:43 +0200 (Do, 27 Aug 2009) | 1 line
#6787: reference fix.
........
r74588 | georg.brandl | 2009-08-30 10:35:01 +0200 (So, 30 Aug 2009) | 1 line
#6803: fix old name.
........
r74603 | georg.brandl | 2009-08-31 08:38:29 +0200 (Mo, 31 Aug 2009) | 1 line
other -> others where multiple arguments are accepted.
........
r74608 | senthil.kumaran | 2009-08-31 18:40:27 +0200 (Mo, 31 Aug 2009) | 3 lines
Doc fix for the issue2637.
........
r74614 | georg.brandl | 2009-09-01 09:40:54 +0200 (Di, 01 Sep 2009) | 1 line
#6813: better documentation for numberless string formats.
........
r74616 | georg.brandl | 2009-09-01 09:46:26 +0200 (Di, 01 Sep 2009) | 1 line
#6808: clarification.
........
r74617 | georg.brandl | 2009-09-01 09:53:37 +0200 (Di, 01 Sep 2009) | 1 line
#6765: hint that log(x, base) is not very sophisticated.
........
r74618 | georg.brandl | 2009-09-01 10:00:47 +0200 (Di, 01 Sep 2009) | 1 line
#6810: add a link to the section about frame objects instead of just a description where to find it.
........
r74631 | georg.brandl | 2009-09-02 22:37:16 +0200 (Mi, 02 Sep 2009) | 1 line
#6821: fix signature of PyBuffer_Release().
........
r74632 | georg.brandl | 2009-09-03 09:27:26 +0200 (Do, 03 Sep 2009) | 1 line
#6828: fix wrongly highlighted blocks.
........
r74633 | georg.brandl | 2009-09-03 14:31:39 +0200 (Do, 03 Sep 2009) | 1 line
#6757: complete the list of types that marshal can serialize.
........
r74652 | georg.brandl | 2009-09-04 13:25:37 +0200 (Fr, 04 Sep 2009) | 1 line
#6756: add some info about the "acct" parameter.
........
r74653 | georg.brandl | 2009-09-04 13:32:18 +0200 (Fr, 04 Sep 2009) | 1 line
#6777: dont discourage usage of Exception.args or promote usage of Exception.message.
........
r74666 | georg.brandl | 2009-09-05 11:04:09 +0200 (Sa, 05 Sep 2009) | 1 line
#6841: remove duplicated word.
........
r74671 | georg.brandl | 2009-09-05 18:47:17 +0200 (Sa, 05 Sep 2009) | 1 line
#6843: add link from filterwarnings to where the meaning of the arguments is covered.
........
r74737 | georg.brandl | 2009-09-09 18:49:13 +0200 (Mi, 09 Sep 2009) | 1 line
Properly document copy and deepcopy as functions.
........
r74739 | georg.brandl | 2009-09-11 09:55:20 +0200 (Fr, 11 Sep 2009) | 1 line
Move function back to its section.
........
r74779 | michael.foord | 2009-09-13 18:13:36 +0200 (So, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74781 | michael.foord | 2009-09-13 18:46:19 +0200 (So, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 19:07:46 +0200 (So, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74784 | georg.brandl | 2009-09-13 20:15:07 +0200 (So, 13 Sep 2009) | 1 line
Typo fix.
........
r74791 | georg.brandl | 2009-09-14 16:08:54 +0200 (Mo, 14 Sep 2009) | 1 line
#6574: list the future features in a table.
........
r74793 | georg.brandl | 2009-09-14 16:50:47 +0200 (Mo, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line
#6880: add reference to classes section in exceptions section, which comes earlier.
........
r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line
#6876: fix base class constructor invocation in example.
........
r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line
#6891: comment out dead link to Unicode article.
........
r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line
#5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign.
........
r74832 | georg.brandl | 2009-09-16 17:57:46 +0200 (Mi, 16 Sep 2009) | 1 line
Rewrap long lines.
........
2009-10-27 11:50:20 -03:00
|
|
|
The value of PI is approximately 3.14159265359.
|
2010-01-05 20:55:21 -04:00
|
|
|
>>> print 'The value of PI is approximately {0!r}.'.format(math.pi)
|
Merged revisions 74492,74531,74545-74550,74553-74555,74588,74603,74608,74614,74616-74618,74631-74633,74652-74653,74666,74671,74737,74739,74779,74781-74782,74784,74791,74793,74818-74820,74822,74832 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74492 | r.david.murray | 2009-08-17 21:26:49 +0200 (Mo, 17 Aug 2009) | 2 lines
Issue 6685: 'toupper' -> 'upper' in cgi doc example explanation.
........
r74531 | vinay.sajip | 2009-08-21 00:04:32 +0200 (Fr, 21 Aug 2009) | 1 line
Added section on exceptions raised during logging.
........
r74545 | georg.brandl | 2009-08-24 19:14:29 +0200 (Mo, 24 Aug 2009) | 1 line
#6772: mention utf-8 as utf8 alias.
........
r74546 | georg.brandl | 2009-08-24 19:20:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6725: spell "namespace" consistently.
........
r74547 | georg.brandl | 2009-08-24 19:22:05 +0200 (Mo, 24 Aug 2009) | 1 line
#6718: fix example.
........
r74548 | georg.brandl | 2009-08-24 19:24:27 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: mention "deleting" as an alias for removing files.
........
r74549 | benjamin.peterson | 2009-08-24 19:42:36 +0200 (Mo, 24 Aug 2009) | 1 line
fix pdf building by teaching latex the right encoding package
........
r74550 | georg.brandl | 2009-08-24 19:48:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: note that rmdir only removes empty directories.
........
r74553 | r.david.murray | 2009-08-27 03:04:59 +0200 (Do, 27 Aug 2009) | 2 lines
Remove leftover text from end of sentence.
........
r74554 | georg.brandl | 2009-08-27 20:59:02 +0200 (Do, 27 Aug 2009) | 1 line
Typo fix.
........
r74555 | georg.brandl | 2009-08-27 21:02:43 +0200 (Do, 27 Aug 2009) | 1 line
#6787: reference fix.
........
r74588 | georg.brandl | 2009-08-30 10:35:01 +0200 (So, 30 Aug 2009) | 1 line
#6803: fix old name.
........
r74603 | georg.brandl | 2009-08-31 08:38:29 +0200 (Mo, 31 Aug 2009) | 1 line
other -> others where multiple arguments are accepted.
........
r74608 | senthil.kumaran | 2009-08-31 18:40:27 +0200 (Mo, 31 Aug 2009) | 3 lines
Doc fix for the issue2637.
........
r74614 | georg.brandl | 2009-09-01 09:40:54 +0200 (Di, 01 Sep 2009) | 1 line
#6813: better documentation for numberless string formats.
........
r74616 | georg.brandl | 2009-09-01 09:46:26 +0200 (Di, 01 Sep 2009) | 1 line
#6808: clarification.
........
r74617 | georg.brandl | 2009-09-01 09:53:37 +0200 (Di, 01 Sep 2009) | 1 line
#6765: hint that log(x, base) is not very sophisticated.
........
r74618 | georg.brandl | 2009-09-01 10:00:47 +0200 (Di, 01 Sep 2009) | 1 line
#6810: add a link to the section about frame objects instead of just a description where to find it.
........
r74631 | georg.brandl | 2009-09-02 22:37:16 +0200 (Mi, 02 Sep 2009) | 1 line
#6821: fix signature of PyBuffer_Release().
........
r74632 | georg.brandl | 2009-09-03 09:27:26 +0200 (Do, 03 Sep 2009) | 1 line
#6828: fix wrongly highlighted blocks.
........
r74633 | georg.brandl | 2009-09-03 14:31:39 +0200 (Do, 03 Sep 2009) | 1 line
#6757: complete the list of types that marshal can serialize.
........
r74652 | georg.brandl | 2009-09-04 13:25:37 +0200 (Fr, 04 Sep 2009) | 1 line
#6756: add some info about the "acct" parameter.
........
r74653 | georg.brandl | 2009-09-04 13:32:18 +0200 (Fr, 04 Sep 2009) | 1 line
#6777: dont discourage usage of Exception.args or promote usage of Exception.message.
........
r74666 | georg.brandl | 2009-09-05 11:04:09 +0200 (Sa, 05 Sep 2009) | 1 line
#6841: remove duplicated word.
........
r74671 | georg.brandl | 2009-09-05 18:47:17 +0200 (Sa, 05 Sep 2009) | 1 line
#6843: add link from filterwarnings to where the meaning of the arguments is covered.
........
r74737 | georg.brandl | 2009-09-09 18:49:13 +0200 (Mi, 09 Sep 2009) | 1 line
Properly document copy and deepcopy as functions.
........
r74739 | georg.brandl | 2009-09-11 09:55:20 +0200 (Fr, 11 Sep 2009) | 1 line
Move function back to its section.
........
r74779 | michael.foord | 2009-09-13 18:13:36 +0200 (So, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74781 | michael.foord | 2009-09-13 18:46:19 +0200 (So, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 19:07:46 +0200 (So, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74784 | georg.brandl | 2009-09-13 20:15:07 +0200 (So, 13 Sep 2009) | 1 line
Typo fix.
........
r74791 | georg.brandl | 2009-09-14 16:08:54 +0200 (Mo, 14 Sep 2009) | 1 line
#6574: list the future features in a table.
........
r74793 | georg.brandl | 2009-09-14 16:50:47 +0200 (Mo, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line
#6880: add reference to classes section in exceptions section, which comes earlier.
........
r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line
#6876: fix base class constructor invocation in example.
........
r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line
#6891: comment out dead link to Unicode article.
........
r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line
#5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign.
........
r74832 | georg.brandl | 2009-09-16 17:57:46 +0200 (Mi, 16 Sep 2009) | 1 line
Rewrap long lines.
........
2009-10-27 11:50:20 -03:00
|
|
|
The value of PI is approximately 3.141592653589793.
|
|
|
|
|
Merged revisions 74008,74021-74022,74074-74075,74077,74148,74179,74188,74192-74194,74200,74205 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74008 | benjamin.peterson | 2009-07-15 02:46:42 +0200 (Mi, 15 Jul 2009) | 1 line
update year
........
r74021 | georg.brandl | 2009-07-16 09:33:04 +0200 (Do, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 09:38:35 +0200 (Do, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74074 | georg.brandl | 2009-07-18 11:03:10 +0200 (Sa, 18 Jul 2009) | 1 line
#6513: fix example code: warning categories are classes, not instances.
........
r74075 | georg.brandl | 2009-07-18 11:06:31 +0200 (Sa, 18 Jul 2009) | 1 line
#6505: fix typos.
........
r74077 | georg.brandl | 2009-07-18 11:43:40 +0200 (Sa, 18 Jul 2009) | 1 line
#6489: fix an ambiguity in getiterator() documentation.
........
r74148 | ezio.melotti | 2009-07-21 22:18:27 +0200 (Di, 21 Jul 2009) | 1 line
#6536 fixed typo
........
r74179 | ezio.melotti | 2009-07-22 23:08:49 +0200 (Mi, 22 Jul 2009) | 1 line
#6423 has_key -> in
........
r74188 | benjamin.peterson | 2009-07-23 16:25:31 +0200 (Do, 23 Jul 2009) | 1 line
use bools
........
r74192 | georg.brandl | 2009-07-24 18:28:38 +0200 (Fr, 24 Jul 2009) | 1 line
Fix arg types of et#.
........
r74193 | georg.brandl | 2009-07-24 18:46:38 +0200 (Fr, 24 Jul 2009) | 1 line
Dont put "void" in signature for nullary functions.
........
r74194 | georg.brandl | 2009-07-24 22:09:46 +0200 (Fr, 24 Jul 2009) | 1 line
#6564: fix section about the two raise syntaxes.
........
r74200 | georg.brandl | 2009-07-25 15:02:15 +0200 (Sa, 25 Jul 2009) | 1 line
#6571: add index entries for more operators.
........
r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line
#6576: fix cross-refs in re docs.
........
2009-10-27 11:34:21 -03:00
|
|
|
An optional ``':'`` and format specifier can follow the field name. This allows
|
2008-05-25 21:54:22 -03:00
|
|
|
greater control over how the value is formatted. The following example
|
Merged revisions 74008,74021-74022,74074-74075,74077,74148,74179,74188,74192-74194,74200,74205 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74008 | benjamin.peterson | 2009-07-15 02:46:42 +0200 (Mi, 15 Jul 2009) | 1 line
update year
........
r74021 | georg.brandl | 2009-07-16 09:33:04 +0200 (Do, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 09:38:35 +0200 (Do, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74074 | georg.brandl | 2009-07-18 11:03:10 +0200 (Sa, 18 Jul 2009) | 1 line
#6513: fix example code: warning categories are classes, not instances.
........
r74075 | georg.brandl | 2009-07-18 11:06:31 +0200 (Sa, 18 Jul 2009) | 1 line
#6505: fix typos.
........
r74077 | georg.brandl | 2009-07-18 11:43:40 +0200 (Sa, 18 Jul 2009) | 1 line
#6489: fix an ambiguity in getiterator() documentation.
........
r74148 | ezio.melotti | 2009-07-21 22:18:27 +0200 (Di, 21 Jul 2009) | 1 line
#6536 fixed typo
........
r74179 | ezio.melotti | 2009-07-22 23:08:49 +0200 (Mi, 22 Jul 2009) | 1 line
#6423 has_key -> in
........
r74188 | benjamin.peterson | 2009-07-23 16:25:31 +0200 (Do, 23 Jul 2009) | 1 line
use bools
........
r74192 | georg.brandl | 2009-07-24 18:28:38 +0200 (Fr, 24 Jul 2009) | 1 line
Fix arg types of et#.
........
r74193 | georg.brandl | 2009-07-24 18:46:38 +0200 (Fr, 24 Jul 2009) | 1 line
Dont put "void" in signature for nullary functions.
........
r74194 | georg.brandl | 2009-07-24 22:09:46 +0200 (Fr, 24 Jul 2009) | 1 line
#6564: fix section about the two raise syntaxes.
........
r74200 | georg.brandl | 2009-07-25 15:02:15 +0200 (Sa, 25 Jul 2009) | 1 line
#6571: add index entries for more operators.
........
r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line
#6576: fix cross-refs in re docs.
........
2009-10-27 11:34:21 -03:00
|
|
|
truncates Pi to three places after the decimal.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> import math
|
2008-05-25 21:54:22 -03:00
|
|
|
>>> print 'The value of PI is approximately {0:.3f}.'.format(math.pi)
|
2007-08-15 11:28:01 -03:00
|
|
|
The value of PI is approximately 3.142.
|
|
|
|
|
2008-05-25 21:54:22 -03:00
|
|
|
Passing an integer after the ``':'`` will cause that field to be a minimum
|
Merged revisions 74210,74239,74252-74253,74256,74258-74261,74332-74333,74404,74411,74445,74465,74467,74488 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74210 | georg.brandl | 2009-07-26 16:44:23 +0200 (So, 26 Jul 2009) | 1 line
Move member descriptions inside the classes.
........
r74239 | georg.brandl | 2009-07-28 20:55:32 +0200 (Di, 28 Jul 2009) | 1 line
Clarify quote_plus() usage.
........
r74252 | georg.brandl | 2009-07-29 18:06:31 +0200 (Mi, 29 Jul 2009) | 1 line
#6593: fix link targets.
........
r74253 | georg.brandl | 2009-07-29 18:09:17 +0200 (Mi, 29 Jul 2009) | 1 line
#6591: add reference to ioctl in fcntl module for platforms other than Windows.
........
r74256 | georg.brandl | 2009-07-29 18:32:30 +0200 (Mi, 29 Jul 2009) | 1 line
#6336: Add nb_divide.
........
r74258 | georg.brandl | 2009-07-29 18:57:05 +0200 (Mi, 29 Jul 2009) | 1 line
Add a link to readline, and mention IPython and bpython.
........
r74259 | georg.brandl | 2009-07-29 19:07:21 +0200 (Mi, 29 Jul 2009) | 1 line
Fix some markup and small factual glitches found by M. Markert.
........
r74260 | georg.brandl | 2009-07-29 19:15:20 +0200 (Mi, 29 Jul 2009) | 1 line
Fix a few markup glitches.
........
r74261 | georg.brandl | 2009-07-29 19:50:25 +0200 (Mi, 29 Jul 2009) | 1 line
Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better.
........
r74332 | georg.brandl | 2009-08-06 19:23:21 +0200 (Do, 06 Aug 2009) | 1 line
Fix punctuation and one copy-paste error.
........
r74333 | georg.brandl | 2009-08-06 19:43:55 +0200 (Do, 06 Aug 2009) | 1 line
#6658: fix two typos.
........
r74404 | georg.brandl | 2009-08-13 14:05:52 +0200 (Do, 13 Aug 2009) | 1 line
Use locale.format_string() for more than one specifier.
........
r74411 | georg.brandl | 2009-08-13 14:57:25 +0200 (Do, 13 Aug 2009) | 2 lines
Remove potentially confusing sentence in __mangling description.
........
r74445 | vinay.sajip | 2009-08-14 13:33:54 +0200 (Fr, 14 Aug 2009) | 1 line
Added versionchanged notices for optional 'delay' parameter to file handler classes.
........
r74465 | vinay.sajip | 2009-08-16 01:23:12 +0200 (So, 16 Aug 2009) | 1 line
Added section on logging to one file from multiple processes.
........
r74467 | vinay.sajip | 2009-08-16 01:34:47 +0200 (So, 16 Aug 2009) | 1 line
Refined section on logging to one file from multiple processes.
........
r74488 | vinay.sajip | 2009-08-17 15:14:37 +0200 (Mo, 17 Aug 2009) | 1 line
Further refined section on logging to one file from multiple processes.
........
2009-10-27 11:41:50 -03:00
|
|
|
number of characters wide. This is useful for making tables pretty. ::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
|
|
|
|
>>> for name, phone in table.items():
|
2008-05-25 21:54:22 -03:00
|
|
|
... print '{0:10} ==> {1:10d}'.format(name, phone)
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
...
|
2007-08-15 11:28:01 -03:00
|
|
|
Jack ==> 4098
|
|
|
|
Dcab ==> 7678
|
|
|
|
Sjoerd ==> 4127
|
|
|
|
|
|
|
|
If you have a really long format string that you don't want to split up, it
|
|
|
|
would be nice if you could reference the variables to be formatted by name
|
2008-05-25 21:54:22 -03:00
|
|
|
instead of by position. This can be done by simply passing the dict and using
|
|
|
|
square brackets ``'[]'`` to access the keys ::
|
|
|
|
|
|
|
|
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
|
2008-07-26 19:13:29 -03:00
|
|
|
>>> print ('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
|
|
|
|
... 'Dcab: {0[Dcab]:d}'.format(table))
|
2008-05-25 21:54:22 -03:00
|
|
|
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
|
|
|
|
|
|
|
|
This could also be done by passing the table as keyword arguments with the '**'
|
Merged revisions 74210,74239,74252-74253,74256,74258-74261,74332-74333,74404,74411,74445,74465,74467,74488 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74210 | georg.brandl | 2009-07-26 16:44:23 +0200 (So, 26 Jul 2009) | 1 line
Move member descriptions inside the classes.
........
r74239 | georg.brandl | 2009-07-28 20:55:32 +0200 (Di, 28 Jul 2009) | 1 line
Clarify quote_plus() usage.
........
r74252 | georg.brandl | 2009-07-29 18:06:31 +0200 (Mi, 29 Jul 2009) | 1 line
#6593: fix link targets.
........
r74253 | georg.brandl | 2009-07-29 18:09:17 +0200 (Mi, 29 Jul 2009) | 1 line
#6591: add reference to ioctl in fcntl module for platforms other than Windows.
........
r74256 | georg.brandl | 2009-07-29 18:32:30 +0200 (Mi, 29 Jul 2009) | 1 line
#6336: Add nb_divide.
........
r74258 | georg.brandl | 2009-07-29 18:57:05 +0200 (Mi, 29 Jul 2009) | 1 line
Add a link to readline, and mention IPython and bpython.
........
r74259 | georg.brandl | 2009-07-29 19:07:21 +0200 (Mi, 29 Jul 2009) | 1 line
Fix some markup and small factual glitches found by M. Markert.
........
r74260 | georg.brandl | 2009-07-29 19:15:20 +0200 (Mi, 29 Jul 2009) | 1 line
Fix a few markup glitches.
........
r74261 | georg.brandl | 2009-07-29 19:50:25 +0200 (Mi, 29 Jul 2009) | 1 line
Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better.
........
r74332 | georg.brandl | 2009-08-06 19:23:21 +0200 (Do, 06 Aug 2009) | 1 line
Fix punctuation and one copy-paste error.
........
r74333 | georg.brandl | 2009-08-06 19:43:55 +0200 (Do, 06 Aug 2009) | 1 line
#6658: fix two typos.
........
r74404 | georg.brandl | 2009-08-13 14:05:52 +0200 (Do, 13 Aug 2009) | 1 line
Use locale.format_string() for more than one specifier.
........
r74411 | georg.brandl | 2009-08-13 14:57:25 +0200 (Do, 13 Aug 2009) | 2 lines
Remove potentially confusing sentence in __mangling description.
........
r74445 | vinay.sajip | 2009-08-14 13:33:54 +0200 (Fr, 14 Aug 2009) | 1 line
Added versionchanged notices for optional 'delay' parameter to file handler classes.
........
r74465 | vinay.sajip | 2009-08-16 01:23:12 +0200 (So, 16 Aug 2009) | 1 line
Added section on logging to one file from multiple processes.
........
r74467 | vinay.sajip | 2009-08-16 01:34:47 +0200 (So, 16 Aug 2009) | 1 line
Refined section on logging to one file from multiple processes.
........
r74488 | vinay.sajip | 2009-08-17 15:14:37 +0200 (Mo, 17 Aug 2009) | 1 line
Further refined section on logging to one file from multiple processes.
........
2009-10-27 11:41:50 -03:00
|
|
|
notation. ::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
|
2008-05-25 21:54:22 -03:00
|
|
|
>>> print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)
|
2007-08-15 11:28:01 -03:00
|
|
|
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
|
|
|
|
|
|
|
|
This is particularly useful in combination with the new built-in :func:`vars`
|
|
|
|
function, which returns a dictionary containing all local variables.
|
|
|
|
|
2009-07-02 12:37:21 -03:00
|
|
|
For a complete overview of string formatting with :meth:`str.format`, see
|
2008-05-25 21:54:22 -03:00
|
|
|
:ref:`formatstrings`.
|
|
|
|
|
|
|
|
|
|
|
|
Old string formatting
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
The ``%`` operator can also be used for string formatting. It interprets the
|
|
|
|
left argument much like a :cfunc:`sprintf`\ -style format string to be applied
|
|
|
|
to the right argument, and returns the string resulting from this formatting
|
|
|
|
operation. For example::
|
|
|
|
|
|
|
|
>>> import math
|
|
|
|
>>> print 'The value of PI is approximately %5.3f.' % math.pi
|
|
|
|
The value of PI is approximately 3.142.
|
|
|
|
|
|
|
|
Since :meth:`str.format` is quite new, a lot of Python code still uses the ``%``
|
Merged revisions 74008,74021-74022,74074-74075,74077,74148,74179,74188,74192-74194,74200,74205 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74008 | benjamin.peterson | 2009-07-15 02:46:42 +0200 (Mi, 15 Jul 2009) | 1 line
update year
........
r74021 | georg.brandl | 2009-07-16 09:33:04 +0200 (Do, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 09:38:35 +0200 (Do, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74074 | georg.brandl | 2009-07-18 11:03:10 +0200 (Sa, 18 Jul 2009) | 1 line
#6513: fix example code: warning categories are classes, not instances.
........
r74075 | georg.brandl | 2009-07-18 11:06:31 +0200 (Sa, 18 Jul 2009) | 1 line
#6505: fix typos.
........
r74077 | georg.brandl | 2009-07-18 11:43:40 +0200 (Sa, 18 Jul 2009) | 1 line
#6489: fix an ambiguity in getiterator() documentation.
........
r74148 | ezio.melotti | 2009-07-21 22:18:27 +0200 (Di, 21 Jul 2009) | 1 line
#6536 fixed typo
........
r74179 | ezio.melotti | 2009-07-22 23:08:49 +0200 (Mi, 22 Jul 2009) | 1 line
#6423 has_key -> in
........
r74188 | benjamin.peterson | 2009-07-23 16:25:31 +0200 (Do, 23 Jul 2009) | 1 line
use bools
........
r74192 | georg.brandl | 2009-07-24 18:28:38 +0200 (Fr, 24 Jul 2009) | 1 line
Fix arg types of et#.
........
r74193 | georg.brandl | 2009-07-24 18:46:38 +0200 (Fr, 24 Jul 2009) | 1 line
Dont put "void" in signature for nullary functions.
........
r74194 | georg.brandl | 2009-07-24 22:09:46 +0200 (Fr, 24 Jul 2009) | 1 line
#6564: fix section about the two raise syntaxes.
........
r74200 | georg.brandl | 2009-07-25 15:02:15 +0200 (Sa, 25 Jul 2009) | 1 line
#6571: add index entries for more operators.
........
r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line
#6576: fix cross-refs in re docs.
........
2009-10-27 11:34:21 -03:00
|
|
|
operator. However, because this old style of formatting will eventually be
|
|
|
|
removed from the language, :meth:`str.format` should generally be used.
|
2008-05-25 21:54:22 -03:00
|
|
|
|
|
|
|
More information can be found in the :ref:`string-formatting` section.
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. _tut-files:
|
|
|
|
|
|
|
|
Reading and Writing Files
|
|
|
|
=========================
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
builtin: open
|
|
|
|
object: file
|
|
|
|
|
|
|
|
:func:`open` returns a file object, and is most commonly used with two
|
|
|
|
arguments: ``open(filename, mode)``.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
>>> f = open('/tmp/workfile', 'w')
|
2007-08-15 11:28:01 -03:00
|
|
|
>>> print f
|
|
|
|
<open file '/tmp/workfile', mode 'w' at 80a0960>
|
|
|
|
|
|
|
|
The first argument is a string containing the filename. The second argument is
|
|
|
|
another string containing a few characters describing the way in which the file
|
|
|
|
will be used. *mode* can be ``'r'`` when the file will only be read, ``'w'``
|
|
|
|
for only writing (an existing file with the same name will be erased), and
|
|
|
|
``'a'`` opens the file for appending; any data written to the file is
|
|
|
|
automatically added to the end. ``'r+'`` opens the file for both reading and
|
|
|
|
writing. The *mode* argument is optional; ``'r'`` will be assumed if it's
|
|
|
|
omitted.
|
|
|
|
|
2008-09-13 14:41:16 -03:00
|
|
|
On Windows, ``'b'`` appended to the mode opens the file in binary mode, so there
|
Merged revisions 74492,74531,74545-74550,74553-74555,74588,74603,74608,74614,74616-74618,74631-74633,74652-74653,74666,74671,74737,74739,74779,74781-74782,74784,74791,74793,74818-74820,74822,74832 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74492 | r.david.murray | 2009-08-17 21:26:49 +0200 (Mo, 17 Aug 2009) | 2 lines
Issue 6685: 'toupper' -> 'upper' in cgi doc example explanation.
........
r74531 | vinay.sajip | 2009-08-21 00:04:32 +0200 (Fr, 21 Aug 2009) | 1 line
Added section on exceptions raised during logging.
........
r74545 | georg.brandl | 2009-08-24 19:14:29 +0200 (Mo, 24 Aug 2009) | 1 line
#6772: mention utf-8 as utf8 alias.
........
r74546 | georg.brandl | 2009-08-24 19:20:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6725: spell "namespace" consistently.
........
r74547 | georg.brandl | 2009-08-24 19:22:05 +0200 (Mo, 24 Aug 2009) | 1 line
#6718: fix example.
........
r74548 | georg.brandl | 2009-08-24 19:24:27 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: mention "deleting" as an alias for removing files.
........
r74549 | benjamin.peterson | 2009-08-24 19:42:36 +0200 (Mo, 24 Aug 2009) | 1 line
fix pdf building by teaching latex the right encoding package
........
r74550 | georg.brandl | 2009-08-24 19:48:40 +0200 (Mo, 24 Aug 2009) | 1 line
#6677: note that rmdir only removes empty directories.
........
r74553 | r.david.murray | 2009-08-27 03:04:59 +0200 (Do, 27 Aug 2009) | 2 lines
Remove leftover text from end of sentence.
........
r74554 | georg.brandl | 2009-08-27 20:59:02 +0200 (Do, 27 Aug 2009) | 1 line
Typo fix.
........
r74555 | georg.brandl | 2009-08-27 21:02:43 +0200 (Do, 27 Aug 2009) | 1 line
#6787: reference fix.
........
r74588 | georg.brandl | 2009-08-30 10:35:01 +0200 (So, 30 Aug 2009) | 1 line
#6803: fix old name.
........
r74603 | georg.brandl | 2009-08-31 08:38:29 +0200 (Mo, 31 Aug 2009) | 1 line
other -> others where multiple arguments are accepted.
........
r74608 | senthil.kumaran | 2009-08-31 18:40:27 +0200 (Mo, 31 Aug 2009) | 3 lines
Doc fix for the issue2637.
........
r74614 | georg.brandl | 2009-09-01 09:40:54 +0200 (Di, 01 Sep 2009) | 1 line
#6813: better documentation for numberless string formats.
........
r74616 | georg.brandl | 2009-09-01 09:46:26 +0200 (Di, 01 Sep 2009) | 1 line
#6808: clarification.
........
r74617 | georg.brandl | 2009-09-01 09:53:37 +0200 (Di, 01 Sep 2009) | 1 line
#6765: hint that log(x, base) is not very sophisticated.
........
r74618 | georg.brandl | 2009-09-01 10:00:47 +0200 (Di, 01 Sep 2009) | 1 line
#6810: add a link to the section about frame objects instead of just a description where to find it.
........
r74631 | georg.brandl | 2009-09-02 22:37:16 +0200 (Mi, 02 Sep 2009) | 1 line
#6821: fix signature of PyBuffer_Release().
........
r74632 | georg.brandl | 2009-09-03 09:27:26 +0200 (Do, 03 Sep 2009) | 1 line
#6828: fix wrongly highlighted blocks.
........
r74633 | georg.brandl | 2009-09-03 14:31:39 +0200 (Do, 03 Sep 2009) | 1 line
#6757: complete the list of types that marshal can serialize.
........
r74652 | georg.brandl | 2009-09-04 13:25:37 +0200 (Fr, 04 Sep 2009) | 1 line
#6756: add some info about the "acct" parameter.
........
r74653 | georg.brandl | 2009-09-04 13:32:18 +0200 (Fr, 04 Sep 2009) | 1 line
#6777: dont discourage usage of Exception.args or promote usage of Exception.message.
........
r74666 | georg.brandl | 2009-09-05 11:04:09 +0200 (Sa, 05 Sep 2009) | 1 line
#6841: remove duplicated word.
........
r74671 | georg.brandl | 2009-09-05 18:47:17 +0200 (Sa, 05 Sep 2009) | 1 line
#6843: add link from filterwarnings to where the meaning of the arguments is covered.
........
r74737 | georg.brandl | 2009-09-09 18:49:13 +0200 (Mi, 09 Sep 2009) | 1 line
Properly document copy and deepcopy as functions.
........
r74739 | georg.brandl | 2009-09-11 09:55:20 +0200 (Fr, 11 Sep 2009) | 1 line
Move function back to its section.
........
r74779 | michael.foord | 2009-09-13 18:13:36 +0200 (So, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74781 | michael.foord | 2009-09-13 18:46:19 +0200 (So, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 19:07:46 +0200 (So, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74784 | georg.brandl | 2009-09-13 20:15:07 +0200 (So, 13 Sep 2009) | 1 line
Typo fix.
........
r74791 | georg.brandl | 2009-09-14 16:08:54 +0200 (Mo, 14 Sep 2009) | 1 line
#6574: list the future features in a table.
........
r74793 | georg.brandl | 2009-09-14 16:50:47 +0200 (Mo, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line
#6880: add reference to classes section in exceptions section, which comes earlier.
........
r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line
#6876: fix base class constructor invocation in example.
........
r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line
#6891: comment out dead link to Unicode article.
........
r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line
#5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign.
........
r74832 | georg.brandl | 2009-09-16 17:57:46 +0200 (Mi, 16 Sep 2009) | 1 line
Rewrap long lines.
........
2009-10-27 11:50:20 -03:00
|
|
|
are also modes like ``'rb'``, ``'wb'``, and ``'r+b'``. Python on Windows makes
|
|
|
|
a distinction between text and binary files; the end-of-line characters in text
|
2008-09-13 14:41:16 -03:00
|
|
|
files are automatically altered slightly when data is read or written. This
|
|
|
|
behind-the-scenes modification to file data is fine for ASCII text files, but
|
|
|
|
it'll corrupt binary data like that in :file:`JPEG` or :file:`EXE` files. Be
|
|
|
|
very careful to use binary mode when reading and writing such files. On Unix,
|
|
|
|
it doesn't hurt to append a ``'b'`` to the mode, so you can use it
|
|
|
|
platform-independently for all binary files.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _tut-filemethods:
|
|
|
|
|
|
|
|
Methods of File Objects
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
The rest of the examples in this section will assume that a file object called
|
|
|
|
``f`` has already been created.
|
|
|
|
|
|
|
|
To read a file's contents, call ``f.read(size)``, which reads some quantity of
|
|
|
|
data and returns it as a string. *size* is an optional numeric argument. When
|
|
|
|
*size* is omitted or negative, the entire contents of the file will be read and
|
|
|
|
returned; it's your problem if the file is twice as large as your machine's
|
|
|
|
memory. Otherwise, at most *size* bytes are read and returned. If the end of
|
|
|
|
the file has been reached, ``f.read()`` will return an empty string (``""``).
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> f.read()
|
|
|
|
'This is the entire file.\n'
|
|
|
|
>>> f.read()
|
|
|
|
''
|
|
|
|
|
|
|
|
``f.readline()`` reads a single line from the file; a newline character (``\n``)
|
|
|
|
is left at the end of the string, and is only omitted on the last line of the
|
|
|
|
file if the file doesn't end in a newline. This makes the return value
|
|
|
|
unambiguous; if ``f.readline()`` returns an empty string, the end of the file
|
|
|
|
has been reached, while a blank line is represented by ``'\n'``, a string
|
|
|
|
containing only a single newline. ::
|
|
|
|
|
|
|
|
>>> f.readline()
|
|
|
|
'This is the first line of the file.\n'
|
|
|
|
>>> f.readline()
|
|
|
|
'Second line of the file\n'
|
|
|
|
>>> f.readline()
|
|
|
|
''
|
|
|
|
|
|
|
|
``f.readlines()`` returns a list containing all the lines of data in the file.
|
|
|
|
If given an optional parameter *sizehint*, it reads that many bytes from the
|
|
|
|
file and enough more to complete a line, and returns the lines from that. This
|
|
|
|
is often used to allow efficient reading of a large file by lines, but without
|
|
|
|
having to load the entire file in memory. Only complete lines will be returned.
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> f.readlines()
|
|
|
|
['This is the first line of the file.\n', 'Second line of the file\n']
|
|
|
|
|
2007-09-20 05:44:59 -03:00
|
|
|
An alternative approach to reading lines is to loop over the file object. This is
|
2007-08-15 11:28:01 -03:00
|
|
|
memory efficient, fast, and leads to simpler code::
|
|
|
|
|
|
|
|
>>> for line in f:
|
|
|
|
print line,
|
|
|
|
|
|
|
|
This is the first line of the file.
|
|
|
|
Second line of the file
|
|
|
|
|
|
|
|
The alternative approach is simpler but does not provide as fine-grained
|
|
|
|
control. Since the two approaches manage line buffering differently, they
|
|
|
|
should not be mixed.
|
|
|
|
|
|
|
|
``f.write(string)`` writes the contents of *string* to the file, returning
|
|
|
|
``None``. ::
|
|
|
|
|
|
|
|
>>> f.write('This is a test\n')
|
|
|
|
|
|
|
|
To write something other than a string, it needs to be converted to a string
|
|
|
|
first::
|
|
|
|
|
|
|
|
>>> value = ('the answer', 42)
|
|
|
|
>>> s = str(value)
|
|
|
|
>>> f.write(s)
|
|
|
|
|
|
|
|
``f.tell()`` returns an integer giving the file object's current position in the
|
|
|
|
file, measured in bytes from the beginning of the file. To change the file
|
|
|
|
object's position, use ``f.seek(offset, from_what)``. The position is computed
|
|
|
|
from adding *offset* to a reference point; the reference point is selected by
|
|
|
|
the *from_what* argument. A *from_what* value of 0 measures from the beginning
|
|
|
|
of the file, 1 uses the current file position, and 2 uses the end of the file as
|
|
|
|
the reference point. *from_what* can be omitted and defaults to 0, using the
|
|
|
|
beginning of the file as the reference point. ::
|
|
|
|
|
|
|
|
>>> f = open('/tmp/workfile', 'r+')
|
|
|
|
>>> f.write('0123456789abcdef')
|
|
|
|
>>> f.seek(5) # Go to the 6th byte in the file
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
>>> f.read(1)
|
2007-08-15 11:28:01 -03:00
|
|
|
'5'
|
|
|
|
>>> f.seek(-3, 2) # Go to the 3rd byte before the end
|
|
|
|
>>> f.read(1)
|
|
|
|
'd'
|
|
|
|
|
|
|
|
When you're done with a file, call ``f.close()`` to close it and free up any
|
|
|
|
system resources taken up by the open file. After calling ``f.close()``,
|
|
|
|
attempts to use the file object will automatically fail. ::
|
|
|
|
|
|
|
|
>>> f.close()
|
|
|
|
>>> f.read()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in ?
|
|
|
|
ValueError: I/O operation on closed file
|
|
|
|
|
2008-07-16 20:35:54 -03:00
|
|
|
It is good practice to use the :keyword:`with` keyword when dealing with file
|
|
|
|
objects. This has the advantage that the file is properly closed after its
|
|
|
|
suite finishes, even if an exception is raised on the way. It is also much
|
|
|
|
shorter than writing equivalent :keyword:`try`\ -\ :keyword:`finally` blocks::
|
|
|
|
|
|
|
|
>>> with open('/tmp/workfile', 'r') as f:
|
|
|
|
... read_data = f.read()
|
|
|
|
>>> f.closed
|
|
|
|
True
|
|
|
|
|
Merged revisions 74210,74239,74252-74253,74256,74258-74261,74332-74333,74404,74411,74445,74465,74467,74488 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74210 | georg.brandl | 2009-07-26 16:44:23 +0200 (So, 26 Jul 2009) | 1 line
Move member descriptions inside the classes.
........
r74239 | georg.brandl | 2009-07-28 20:55:32 +0200 (Di, 28 Jul 2009) | 1 line
Clarify quote_plus() usage.
........
r74252 | georg.brandl | 2009-07-29 18:06:31 +0200 (Mi, 29 Jul 2009) | 1 line
#6593: fix link targets.
........
r74253 | georg.brandl | 2009-07-29 18:09:17 +0200 (Mi, 29 Jul 2009) | 1 line
#6591: add reference to ioctl in fcntl module for platforms other than Windows.
........
r74256 | georg.brandl | 2009-07-29 18:32:30 +0200 (Mi, 29 Jul 2009) | 1 line
#6336: Add nb_divide.
........
r74258 | georg.brandl | 2009-07-29 18:57:05 +0200 (Mi, 29 Jul 2009) | 1 line
Add a link to readline, and mention IPython and bpython.
........
r74259 | georg.brandl | 2009-07-29 19:07:21 +0200 (Mi, 29 Jul 2009) | 1 line
Fix some markup and small factual glitches found by M. Markert.
........
r74260 | georg.brandl | 2009-07-29 19:15:20 +0200 (Mi, 29 Jul 2009) | 1 line
Fix a few markup glitches.
........
r74261 | georg.brandl | 2009-07-29 19:50:25 +0200 (Mi, 29 Jul 2009) | 1 line
Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better.
........
r74332 | georg.brandl | 2009-08-06 19:23:21 +0200 (Do, 06 Aug 2009) | 1 line
Fix punctuation and one copy-paste error.
........
r74333 | georg.brandl | 2009-08-06 19:43:55 +0200 (Do, 06 Aug 2009) | 1 line
#6658: fix two typos.
........
r74404 | georg.brandl | 2009-08-13 14:05:52 +0200 (Do, 13 Aug 2009) | 1 line
Use locale.format_string() for more than one specifier.
........
r74411 | georg.brandl | 2009-08-13 14:57:25 +0200 (Do, 13 Aug 2009) | 2 lines
Remove potentially confusing sentence in __mangling description.
........
r74445 | vinay.sajip | 2009-08-14 13:33:54 +0200 (Fr, 14 Aug 2009) | 1 line
Added versionchanged notices for optional 'delay' parameter to file handler classes.
........
r74465 | vinay.sajip | 2009-08-16 01:23:12 +0200 (So, 16 Aug 2009) | 1 line
Added section on logging to one file from multiple processes.
........
r74467 | vinay.sajip | 2009-08-16 01:34:47 +0200 (So, 16 Aug 2009) | 1 line
Refined section on logging to one file from multiple processes.
........
r74488 | vinay.sajip | 2009-08-17 15:14:37 +0200 (Mo, 17 Aug 2009) | 1 line
Further refined section on logging to one file from multiple processes.
........
2009-10-27 11:41:50 -03:00
|
|
|
File objects have some additional methods, such as :meth:`~file.isatty` and
|
|
|
|
:meth:`~file.truncate` which are less frequently used; consult the Library
|
|
|
|
Reference for a complete guide to file objects.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _tut-pickle:
|
|
|
|
|
|
|
|
The :mod:`pickle` Module
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
.. index:: module: pickle
|
|
|
|
|
|
|
|
Strings can easily be written to and read from a file. Numbers take a bit more
|
|
|
|
effort, since the :meth:`read` method only returns strings, which will have to
|
|
|
|
be passed to a function like :func:`int`, which takes a string like ``'123'``
|
|
|
|
and returns its numeric value 123. However, when you want to save more complex
|
|
|
|
data types like lists, dictionaries, or class instances, things get a lot more
|
|
|
|
complicated.
|
|
|
|
|
|
|
|
Rather than have users be constantly writing and debugging code to save
|
|
|
|
complicated data types, Python provides a standard module called :mod:`pickle`.
|
|
|
|
This is an amazing module that can take almost any Python object (even some
|
|
|
|
forms of Python code!), and convert it to a string representation; this process
|
|
|
|
is called :dfn:`pickling`. Reconstructing the object from the string
|
|
|
|
representation is called :dfn:`unpickling`. Between pickling and unpickling,
|
|
|
|
the string representing the object may have been stored in a file or data, or
|
|
|
|
sent over a network connection to some distant machine.
|
|
|
|
|
|
|
|
If you have an object ``x``, and a file object ``f`` that's been opened for
|
|
|
|
writing, the simplest way to pickle the object takes only one line of code::
|
|
|
|
|
|
|
|
pickle.dump(x, f)
|
|
|
|
|
|
|
|
To unpickle the object again, if ``f`` is a file object which has been opened
|
|
|
|
for reading::
|
|
|
|
|
|
|
|
x = pickle.load(f)
|
|
|
|
|
|
|
|
(There are other variants of this, used when pickling many objects or when you
|
|
|
|
don't want to write the pickled data to a file; consult the complete
|
|
|
|
documentation for :mod:`pickle` in the Python Library Reference.)
|
|
|
|
|
|
|
|
:mod:`pickle` is the standard way to make Python objects which can be stored and
|
|
|
|
reused by other programs or by a future invocation of the same program; the
|
|
|
|
technical term for this is a :dfn:`persistent` object. Because :mod:`pickle` is
|
|
|
|
so widely used, many authors who write Python extensions take care to ensure
|
|
|
|
that new data types such as matrices can be properly pickled and unpickled.
|
|
|
|
|
|
|
|
|