mirror of
https://github.com/python/cpython.git
synced 2025-11-14 07:49:28 +00:00
Issue #11426: use 'with' statements on open files in CSV examples
This commit is contained in:
parent
30178068d9
commit
9cc6249dee
1 changed files with 26 additions and 20 deletions
|
|
@ -400,21 +400,24 @@ Examples
|
||||||
The simplest example of reading a CSV file::
|
The simplest example of reading a CSV file::
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
reader = csv.reader(open("some.csv", newline=''))
|
with open('some.csv', newline='') as f:
|
||||||
|
reader = csv.reader(f)
|
||||||
for row in reader:
|
for row in reader:
|
||||||
print(row)
|
print(row)
|
||||||
|
|
||||||
Reading a file with an alternate format::
|
Reading a file with an alternate format::
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
reader = csv.reader(open("passwd"), delimiter=':', quoting=csv.QUOTE_NONE)
|
with open('passwd') as f:
|
||||||
|
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
|
||||||
for row in reader:
|
for row in reader:
|
||||||
print(row)
|
print(row)
|
||||||
|
|
||||||
The corresponding simplest possible writing example is::
|
The corresponding simplest possible writing example is::
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
writer = csv.writer(open("some.csv", "w"))
|
with open('some.csv', 'w') as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
writer.writerows(someiterable)
|
writer.writerows(someiterable)
|
||||||
|
|
||||||
Since :func:`open` is used to open a CSV file for reading, the file
|
Since :func:`open` is used to open a CSV file for reading, the file
|
||||||
|
|
@ -423,7 +426,8 @@ encoding (see :func:`locale.getpreferredencoding`). To decode a file
|
||||||
using a different encoding, use the ``encoding`` argument of open::
|
using a different encoding, use the ``encoding`` argument of open::
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
reader = csv.reader(open("some.csv", newline='', encoding='utf-8'))
|
with open('some.csv', newline='', encoding='utf-8') as f:
|
||||||
|
reader = csv.reader(f)
|
||||||
for row in reader:
|
for row in reader:
|
||||||
print(row)
|
print(row)
|
||||||
|
|
||||||
|
|
@ -434,13 +438,15 @@ Registering a new dialect::
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
|
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::
|
A slightly more advanced use of the reader --- catching and reporting errors::
|
||||||
|
|
||||||
import csv, sys
|
import csv, sys
|
||||||
filename = "some.csv"
|
filename = 'some.csv'
|
||||||
reader = csv.reader(open(filename, newline=''))
|
with open(filename, newline='') as f:
|
||||||
|
reader = csv.reader(f)
|
||||||
try:
|
try:
|
||||||
for row in reader:
|
for row in reader:
|
||||||
print(row)
|
print(row)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue