Changeset 391 for python/trunk/Doc/library/csv.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/csv.rst
r2 r391 41 41 there are currently some issues regarding ASCII NUL characters. Accordingly, 42 42 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`. 44 44 45 45 … … 58 58 59 59 60 .. function:: reader(csvfile [, dialect='excel'][, fmtparam])60 .. function:: reader(csvfile, dialect='excel', **fmtparams) 61 61 62 62 Return a reader object which will iterate over lines in the given *csvfile*. … … 68 68 specific to a particular CSV dialect. It may be an instance of a subclass of 69 69 the :class:`Dialect` class or one of the strings returned by the 70 :func:`list_dialects` function. The other optional *fmtparam * keyword arguments70 :func:`list_dialects` function. The other optional *fmtparams* keyword arguments 71 71 can be given to override individual formatting parameters in the current 72 72 dialect. For full details about the dialect and formatting parameters, see … … 79 79 80 80 >>> 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) 84 85 Spam, Spam, Spam, Spam, Spam, Baked Beans 85 86 Spam, Lovely Spam, Wonderful Spam … … 95 96 96 97 97 .. function:: writer(csvfile [, dialect='excel'][, fmtparam])98 .. function:: writer(csvfile, dialect='excel', **fmtparams) 98 99 99 100 Return a writer object responsible for converting the user's data into delimited … … 104 105 particular CSV dialect. It may be an instance of a subclass of the 105 106 :class:`Dialect` class or one of the strings returned by the 106 :func:`list_dialects` function. The other optional *fmtparam * keyword arguments107 :func:`list_dialects` function. The other optional *fmtparams* keyword arguments 107 108 can be given to override individual formatting parameters in the current 108 109 dialect. For full details about the dialect and formatting parameters, see … … 116 117 A short usage example:: 117 118 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) 126 128 127 129 Associate *dialect* with *name*. *name* must be a string or Unicode object. The 128 130 dialect can be specified either by passing a sub-class of :class:`Dialect`, or 129 by *fmtparam * keyword arguments, or both, with keyword arguments overriding131 by *fmtparams* keyword arguments, or both, with keyword arguments overriding 130 132 parameters of the dialect. For full details about the dialect and formatting 131 133 parameters, see section :ref:`csv-fmt-params`. … … 163 165 164 166 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) 166 168 167 169 Create an object which operates like a regular reader but maps the information … … 176 178 177 179 178 .. class:: DictWriter(csvfile, fieldnames [, restval=''[, extrasaction='raise'[, dialect='excel'[, *args, **kwds]]]])180 .. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds) 179 181 180 182 Create an object which operates like a regular writer but maps dictionaries onto … … 220 222 The :class:`Sniffer` class provides two methods: 221 223 222 .. method:: sniff(sample [, delimiters=None])224 .. method:: sniff(sample, delimiters=None) 223 225 224 226 Analyze the given *sample* and return a :class:`Dialect` subclass … … 235 237 An example for :class:`Sniffer` use:: 236 238 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 ... 242 244 243 245 … … 354 356 355 357 358 .. attribute:: Dialect.strict 359 360 When ``True``, raise exception :exc:`Error` on bad CSV input. 361 The default is ``False``. 362 356 363 Reader Objects 357 364 -------------- … … 425 432 426 433 434 DictWriter 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 427 444 .. _csv-examples: 428 445 … … 433 450 434 451 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) 466 454 for row in reader: 467 455 print row 468 except csv.Error, e: 469 sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) 456 457 Reading 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 465 The 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 472 Registering 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 479 A 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)) 470 490 471 491 And while the module doesn't directly support parsing strings, it can easily be
Note:
See TracChangeset
for help on using the changeset viewer.