[Python-checkins] (merge 3.1 -> 3.2): merge from 3.1

eli.bendersky python-checkins at python.org
Fri Mar 11 15:44:00 CET 2011


http://hg.python.org/cpython/rev/3b01044ca36f
changeset:   68372:3b01044ca36f
branch:      3.2
parent:      68365:5fdb9f88be50
parent:      68371:d54e9c4fcd43
user:        Eli Bendersky <eliben at gmail.com>
date:        Fri Mar 11 16:39:04 2011 +0200
summary:
  merge from 3.1

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
@@ -419,32 +419,36 @@
 The simplest example of reading a CSV file::
 
    import csv
-   reader = csv.reader(open("some.csv", newline=''))
-   for row in reader:
-       print(row)
+   with open('some.csv', newline='') as f:
+       reader = csv.reader(f)
+       for row in reader:
+           print(row)
 
 Reading a file with an alternate format::
 
    import csv
-   reader = csv.reader(open("passwd"), delimiter=':', quoting=csv.QUOTE_NONE)
-   for row in reader:
-       print(row)
+   with open('passwd') as f:
+       reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
+       for row in reader:
+           print(row)
 
 The corresponding simplest possible writing example is::
 
    import csv
-   writer = csv.writer(open("some.csv", "w"))
-   writer.writerows(someiterable)
+   with open('some.csv', 'w') as f:
+       writer = csv.writer(f)
+       writer.writerows(someiterable)
 
 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::
 
-    import csv
-    reader = csv.reader(open("some.csv", newline='', encoding='utf-8'))
-    for row in reader:
-        print(row)
+   import csv
+   with open('some.csv', newline='', encoding='utf-8') as f:
+       reader = csv.reader(f)
+       for row in reader:
+           print(row)
 
 The same applies to writing in something other than the system default
 encoding: specify the encoding argument when opening the output file.
@@ -453,18 +457,20 @@
 
    import csv
    csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
-   reader = csv.reader(open("passwd"), 'unixpwd')
+   with open('passwd') as f:
+       reader = csv.reader(f, 'unixpwd')
 
 A slightly more advanced use of the reader --- catching and reporting errors::
 
    import csv, sys
-   filename = "some.csv"
-   reader = csv.reader(open(filename, newline=''))
-   try:
-       for row in reader:
-           print(row)
-   except csv.Error as e:
-       sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
+   filename = 'some.csv'
+   with open(filename, newline='') as f:
+       reader = csv.reader(f)
+       try:
+           for row in reader:
+               print(row)
+       except csv.Error as e:
+           sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
 
 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