[Python-checkins] cpython: sigh - i don't like this workflow at all (original) (raw)

skip.montanaro python-checkins at python.org
Sat Mar 19 19:08:12 CET 2011


http://hg.python.org/cpython/rev/145c3703ce48 changeset: 68685:145c3703ce48 user: Skip Montanaro <skip at pobox.com> date: Sat Mar 19 13:05:58 2011 -0500 summary: sigh - i don't like this workflow at all

files: Doc/library/csv.rst

diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -418,101 +418,50 @@

The simplest example of reading a CSV file::

-<<<<<<< local

-======= import csv with open('some.csv', newline='') as f: reader = csv.reader(f) for row in reader: print(row) ->>>>>>> other

Reading a file with an alternate format::

-<<<<<<< local

-======= import csv with open('passwd') as f: reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE) for row in reader: print(row) ->>>>>>> other

The corresponding simplest possible writing example is::

-<<<<<<< local

->>>>>>> other

Since :func:open is used to open a CSV file for reading, the file will by default be decoded into unicode using the system default encoding (see :func:locale.getpreferredencoding). To decode a file using a different encoding, use the encoding argument of open::

-<<<<<<< local

-======= import csv with open('some.csv', newline='', encoding='utf-8') as f: reader = csv.reader(f) for row in reader: print(row) ->>>>>>> other

The same applies to writing in something other than the system default encoding: specify the encoding argument when opening the output file.

Registering a new dialect::

-<<<<<<< local

-======= import csv csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE) with open('passwd') as f: reader = csv.reader(f, 'unixpwd') ->>>>>>> other

A slightly more advanced use of the reader --- catching and reporting errors::

-<<<<<<< local

-======= import csv, sys filename = 'some.csv' with open(filename, newline='') as f: @@ -522,7 +471,6 @@ print(row) except csv.Error as e: sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e)) ->>>>>>> other

And while the module doesn't directly support parsing strings, it can easily be done::

-- Repository URL: http://hg.python.org/cpython



More information about the Python-checkins mailing list