Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cc6249

Browse files
committed
Issue #11426: use 'with' statements on open files in CSV examples
1 parent 3017806 commit 9cc6249

1 file changed

Lines changed: 26 additions & 20 deletions

File tree

Doc/library/csv.rst

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,32 +400,36 @@ Examples
400400
The simplest example of reading a CSV file::
401401

402402
import csv
403-
reader = csv.reader(open("some.csv", newline=''))
404-
for row in reader:
405-
print(row)
403+
with open('some.csv', newline='') as f:
404+
reader = csv.reader(f)
405+
for row in reader:
406+
print(row)
406407

407408
Reading a file with an alternate format::
408409

409410
import csv
410-
reader = csv.reader(open("passwd"), delimiter=':', quoting=csv.QUOTE_NONE)
411-
for row in reader:
412-
print(row)
411+
with open('passwd') as f:
412+
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
413+
for row in reader:
414+
print(row)
413415

414416
The corresponding simplest possible writing example is::
415417

416418
import csv
417-
writer = csv.writer(open("some.csv", "w"))
418-
writer.writerows(someiterable)
419+
with open('some.csv', 'w') as f:
420+
writer = csv.writer(f)
421+
writer.writerows(someiterable)
419422

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

425-
import csv
426-
reader = csv.reader(open("some.csv", newline='', encoding='utf-8'))
427-
for row in reader:
428-
print(row)
428+
import csv
429+
with open('some.csv', newline='', encoding='utf-8') as f:
430+
reader = csv.reader(f)
431+
for row in reader:
432+
print(row)
429433

430434
The same applies to writing in something other than the system default
431435
encoding: specify the encoding argument when opening the output file.
@@ -434,18 +438,20 @@ Registering a new dialect::
434438

435439
import csv
436440
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
437-
reader = csv.reader(open("passwd"), 'unixpwd')
441+
with open('passwd') as f:
442+
reader = csv.reader(f, 'unixpwd')
438443

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

441446
import csv, sys
442-
filename = "some.csv"
443-
reader = csv.reader(open(filename, newline=''))
444-
try:
445-
for row in reader:
446-
print(row)
447-
except csv.Error as e:
448-
sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
447+
filename = 'some.csv'
448+
with open(filename, newline='') as f:
449+
reader = csv.reader(f)
450+
try:
451+
for row in reader:
452+
print(row)
453+
except csv.Error as e:
454+
sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
449455

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

0 commit comments

Comments
 (0)