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/library/csv.rst

    r2 r391  
    4141   there are currently some issues regarding ASCII NUL characters.  Accordingly,
    4242   all input should be UTF-8 or printable ASCII to be safe; see the examples in
    43    section :ref:`csv-examples`. These restrictions will be removed in the future.
     43   section :ref:`csv-examples`.
    4444
    4545
     
    5858
    5959
    60 .. function:: reader(csvfile[, dialect='excel'][, fmtparam])
     60.. function:: reader(csvfile, dialect='excel', **fmtparams)
    6161
    6262   Return a reader object which will iterate over lines in the given *csvfile*.
     
    6868   specific to a particular CSV dialect.  It may be an instance of a subclass of
    6969   the :class:`Dialect` class or one of the strings returned by the
    70    :func:`list_dialects` function.  The other optional *fmtparam* keyword arguments
     70   :func:`list_dialects` function.  The other optional *fmtparams* keyword arguments
    7171   can be given to override individual formatting parameters in the current
    7272   dialect.  For full details about the dialect and formatting parameters, see
     
    7979
    8080      >>> import csv
    81       >>> spamReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|')
    82       >>> for row in spamReader:
    83       ...     print ', '.join(row)
     81      >>> with open('eggs.csv', 'rb') as csvfile:
     82      ...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
     83      ...     for row in spamreader:
     84      ...         print ', '.join(row)
    8485      Spam, Spam, Spam, Spam, Spam, Baked Beans
    8586      Spam, Lovely Spam, Wonderful Spam
     
    9596
    9697
    97 .. function:: writer(csvfile[, dialect='excel'][, fmtparam])
     98.. function:: writer(csvfile, dialect='excel', **fmtparams)
    9899
    99100   Return a writer object responsible for converting the user's data into delimited
     
    104105   particular CSV dialect.  It may be an instance of a subclass of the
    105106   :class:`Dialect` class or one of the strings returned by the
    106    :func:`list_dialects` function.  The other optional *fmtparam* keyword arguments
     107   :func:`list_dialects` function.  The other optional *fmtparams* keyword arguments
    107108   can be given to override individual formatting parameters in the current
    108109   dialect.  For full details about the dialect and formatting parameters, see
     
    116117   A short usage example::
    117118
    118       >>> import csv
    119       >>> spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ',
    120       ...                         quotechar='|', quoting=csv.QUOTE_MINIMAL)
    121       >>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    122       >>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    123 
    124 
    125 .. function:: register_dialect(name[, dialect][, fmtparam])
     119      import csv
     120      with open('eggs.csv', 'wb') as csvfile:
     121          spamwriter = csv.writer(csvfile, delimiter=' ',
     122                                  quotechar='|', quoting=csv.QUOTE_MINIMAL)
     123          spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
     124          spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
     125
     126
     127.. function:: register_dialect(name[, dialect], **fmtparams)
    126128
    127129   Associate *dialect* with *name*.  *name* must be a string or Unicode object. The
    128130   dialect can be specified either by passing a sub-class of :class:`Dialect`, or
    129    by *fmtparam* keyword arguments, or both, with keyword arguments overriding
     131   by *fmtparams* keyword arguments, or both, with keyword arguments overriding
    130132   parameters of the dialect. For full details about the dialect and formatting
    131133   parameters, see section :ref:`csv-fmt-params`.
     
    163165
    164166
    165 .. class:: DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])
     167.. class:: DictReader(csvfile, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
    166168
    167169   Create an object which operates like a regular reader but maps the information
     
    176178
    177179
    178 .. class:: DictWriter(csvfile, fieldnames[, restval=''[, extrasaction='raise'[, dialect='excel'[, *args, **kwds]]]])
     180.. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
    179181
    180182   Create an object which operates like a regular writer but maps dictionaries onto
     
    220222   The :class:`Sniffer` class provides two methods:
    221223
    222    .. method:: sniff(sample[, delimiters=None])
     224   .. method:: sniff(sample, delimiters=None)
    223225
    224226      Analyze the given *sample* and return a :class:`Dialect` subclass
     
    235237An example for :class:`Sniffer` use::
    236238
    237    csvfile = open("example.csv")
    238    dialect = csv.Sniffer().sniff(csvfile.read(1024))
    239    csvfile.seek(0)
    240    reader = csv.reader(csvfile, dialect)
    241    # ... process CSV file contents here ...
     239   with open('example.csv', 'rb') as csvfile:
     240       dialect = csv.Sniffer().sniff(csvfile.read(1024))
     241       csvfile.seek(0)
     242       reader = csv.reader(csvfile, dialect)
     243       # ... process CSV file contents here ...
    242244
    243245
     
    354356
    355357
     358.. attribute:: Dialect.strict
     359
     360   When ``True``, raise exception :exc:`Error` on bad CSV input.
     361   The default is ``False``.
     362
    356363Reader Objects
    357364--------------
     
    425432
    426433
     434DictWriter objects have the following public method:
     435
     436
     437.. method:: DictWriter.writeheader()
     438
     439   Write a row with the field names (as specified in the constructor).
     440
     441   .. versionadded:: 2.7
     442
     443
    427444.. _csv-examples:
    428445
     
    433450
    434451   import csv
    435    reader = csv.reader(open("some.csv", "rb"))
    436    for row in reader:
    437        print row
    438 
    439 Reading a file with an alternate format::
    440 
    441    import csv
    442    reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
    443    for row in reader:
    444        print row
    445 
    446 The corresponding simplest possible writing example is::
    447 
    448    import csv
    449    writer = csv.writer(open("some.csv", "wb"))
    450    writer.writerows(someiterable)
    451 
    452 Registering a new dialect::
    453 
    454    import csv
    455 
    456    csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
    457 
    458    reader = csv.reader(open("passwd", "rb"), 'unixpwd')
    459 
    460 A slightly more advanced use of the reader --- catching and reporting errors::
    461 
    462    import csv, sys
    463    filename = "some.csv"
    464    reader = csv.reader(open(filename, "rb"))
    465    try:
     452   with open('some.csv', 'rb') as f:
     453       reader = csv.reader(f)
    466454       for row in reader:
    467455           print row
    468    except csv.Error, e:
    469        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
     456
     457Reading a file with an alternate format::
     458
     459   import csv
     460   with open('passwd', 'rb') as f:
     461       reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
     462       for row in reader:
     463           print row
     464
     465The corresponding simplest possible writing example is::
     466
     467   import csv
     468   with open('some.csv', 'wb') as f:
     469       writer = csv.writer(f)
     470       writer.writerows(someiterable)
     471
     472Registering a new dialect::
     473
     474   import csv
     475   csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
     476   with open('passwd', 'rb') as f:
     477       reader = csv.reader(f, 'unixpwd')
     478
     479A slightly more advanced use of the reader --- catching and reporting errors::
     480
     481   import csv, sys
     482   filename = 'some.csv'
     483   with open(filename, 'rb') as f:
     484       reader = csv.reader(f)
     485       try:
     486           for row in reader:
     487               print row
     488       except csv.Error as e:
     489           sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
    470490
    471491And while the module doesn't directly support parsing strings, it can easily be
Note: See TracChangeset for help on using the changeset viewer.