Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/tutorial/inputoutput.rst

    r2 r391  
    2020See the Library Reference for more information on this.)
    2121
    22 .. index:: module: string
    23 
    2422Often you'll want more control over the formatting of your output than simply
    2523printing space-separated values.  There are two ways to format your output; the
    2624first way is to do all the string handling yourself; using string slicing and
    2725concatenation operations you can create any layout you can imagine.  The
    28 standard module :mod:`string` contains some useful operations for padding
     26string types have some methods that perform useful operations for padding
    2927strings to a given column width; these will be discussed shortly.  The second
    3028way is to use the :meth:`str.format` method.
     29
     30The :mod:`string` module contains a :class:`~string.Template` class which offers
     31yet another way to substitute values into strings.
    3132
    3233One question remains, of course: how do you convert values to strings? Luckily,
     
    3738fairly human-readable, while :func:`repr` is meant to generate representations
    3839which can be read by the interpreter (or will force a :exc:`SyntaxError` if
    39 there is not equivalent syntax).  For objects which don't have a particular
     40there is no equivalent syntax).  For objects which don't have a particular
    4041representation for human consumption, :func:`str` will return the same value as
    4142:func:`repr`.  Many values, such as numbers or structures like lists and
     
    5051   >>> repr(s)
    5152   "'Hello, world.'"
    52    >>> str(0.1)
    53    '0.1'
    54    >>> repr(0.1)
    55    '0.10000000000000001'
     53   >>> str(1.0/7.0)
     54   '0.142857142857'
     55   >>> repr(1.0/7.0)
     56   '0.14285714285714285'
    5657   >>> x = 10 * 3.25
    5758   >>> y = 200 * 200
     
    103104way :keyword:`print` works: it always adds spaces between its arguments.)
    104105
    105 This example demonstrates the :meth:`rjust` method of string objects, which
    106 right-justifies a string in a field of a given width by padding it with spaces
    107 on the left.  There are similar methods :meth:`ljust` and :meth:`center`.  These
    108 methods do not write anything, they just return a new string.  If the input
    109 string is too long, they don't truncate it, but return it unchanged; this will
    110 mess up your column lay-out but that's usually better than the alternative,
    111 which would be lying about a value.  (If you really want truncation you can
    112 always add a slice operation, as in ``x.ljust(n)[:n]``.)
    113 
    114 There is another method, :meth:`zfill`, which pads a numeric string on the left
    115 with zeros.  It understands about plus and minus signs::
     106This example demonstrates the :meth:`str.rjust` method of string
     107objects, which right-justifies a string in a field of a given width by padding
     108it with spaces on the left.  There are similar methods :meth:`str.ljust` and
     109:meth:`str.center`.  These methods do not write anything, they just return a
     110new string.  If the input string is too long, they don't truncate it, but
     111return it unchanged; this will mess up your column lay-out but that's usually
     112better than the alternative, which would be lying about a value.  (If you
     113really want truncation you can always add a slice operation, as in
     114``x.ljust(n)[:n]``.)
     115
     116There is another method, :meth:`str.zfill`, which pads a numeric string on the
     117left with zeros.  It understands about plus and minus signs::
    116118
    117119   >>> '12'.zfill(5)
     
    124126Basic usage of the :meth:`str.format` method looks like this::
    125127
    126    >>> print 'We are the {0} who say "{1}!"'.format('knights', 'Ni')
     128   >>> print 'We are the {} who say "{}!"'.format('knights', 'Ni')
    127129   We are the knights who say "Ni!"
    128130
    129131The brackets and characters within them (called format fields) are replaced with
    130 the objects passed into the :meth:`~str.format` method.  A number in the
     132the objects passed into the :meth:`str.format` method.  A number in the
    131133brackets refers to the position of the object passed into the
    132 :meth:`~str.format` method. ::
     134:meth:`str.format` method. ::
    133135
    134136   >>> print '{0} and {1}'.format('spam', 'eggs')
     
    137139   eggs and spam
    138140
    139 If keyword arguments are used in the :meth:`~str.format` method, their values
     141If keyword arguments are used in the :meth:`str.format` method, their values
    140142are referred to by using the name of the argument. ::
    141143
     
    154156
    155157   >>> import math
    156    >>> print 'The value of PI is approximately {0}.'.format(math.pi)
     158   >>> print 'The value of PI is approximately {}.'.format(math.pi)
    157159   The value of PI is approximately 3.14159265359.
    158    >>> print 'The value of PI is approximately {0!r}.'.format(math.pi)
     160   >>> print 'The value of PI is approximately {!r}.'.format(math.pi)
    159161   The value of PI is approximately 3.141592653589793.
    160162
    161163An optional ``':'`` and format specifier can follow the field name. This allows
    162164greater control over how the value is formatted.  The following example
    163 truncates Pi to three places after the decimal.
     165rounds Pi to three places after the decimal.
    164166
    165167   >>> import math
     
    195197   Jack: 4098; Sjoerd: 4127; Dcab: 8637678
    196198
    197 This is particularly useful in combination with the new built-in :func:`vars`
    198 function, which returns a dictionary containing all local variables.
     199This is particularly useful in combination with the built-in function
     200:func:`vars`, which returns a dictionary containing all local variables.
    199201
    200202For a complete overview of string formatting with :meth:`str.format`, see
     
    206208
    207209The ``%`` operator can also be used for string formatting. It interprets the
    208 left argument much like a :cfunc:`sprintf`\ -style format string to be applied
     210left argument much like a :c:func:`sprintf`\ -style format string to be applied
    209211to the right argument, and returns the string resulting from this formatting
    210212operation. For example::
     
    214216   The value of PI is approximately 3.142.
    215217
    216 Since :meth:`str.format` is quite new, a lot of Python code still uses the ``%``
    217 operator. However, because this old style of formatting will eventually be
    218 removed from the language, :meth:`str.format` should generally be used.
    219 
    220218More information can be found in the :ref:`string-formatting` section.
    221219
     
    235233::
    236234
    237    >>> f = open('/tmp/workfile', 'w')
     235   >>> f = open('workfile', 'w')
    238236   >>> print f
    239    <open file '/tmp/workfile', mode 'w' at 80a0960>
     237   <open file 'workfile', mode 'w' at 80a0960>
    240238
    241239The first argument is a string containing the filename.  The second argument is
     
    294292   ''
    295293
    296 ``f.readlines()`` returns a list containing all the lines of data in the file.
    297 If given an optional parameter *sizehint*, it reads that many bytes from the
    298 file and enough more to complete a line, and returns the lines from that.  This
    299 is often used to allow efficient reading of a large file by lines, but without
    300 having to load the entire file in memory.  Only complete lines will be returned.
    301 ::
    302 
    303    >>> f.readlines()
    304    ['This is the first line of the file.\n', 'Second line of the file\n']
    305 
    306 An alternative approach to reading lines is to loop over the file object. This is
    307 memory efficient, fast, and leads to simpler code::
     294For reading lines from a file, you can loop over the file object. This is memory
     295efficient, fast, and leads to simple code::
    308296
    309297   >>> for line in f:
     
    313301   Second line of the file
    314302
    315 The alternative approach is simpler but does not provide as fine-grained
    316 control.  Since the two approaches manage line buffering differently, they
    317 should not be mixed.
     303If you want to read all the lines of a file in a list you can also use
     304``list(f)`` or ``f.readlines()``.
    318305
    319306``f.write(string)`` writes the contents of *string* to the file, returning
     
    338325beginning of the file as the reference point. ::
    339326
    340    >>> f = open('/tmp/workfile', 'r+')
     327   >>> f = open('workfile', 'r+')
    341328   >>> f.write('0123456789abcdef')
    342329   >>> f.seek(5)     # Go to the 6th byte in the file
     
    362349shorter than writing equivalent :keyword:`try`\ -\ :keyword:`finally` blocks::
    363350
    364     >>> with open('/tmp/workfile', 'r') as f:
     351    >>> with open('workfile', 'r') as f:
    365352    ...     read_data = f.read()
    366353    >>> f.closed
Note: See TracChangeset for help on using the changeset viewer.