Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9cc6249d
Commit
9cc6249d
authored
Mar 11, 2011
by
Eli Bendersky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #11426: use 'with' statements on open files in CSV examples
parent
30178068
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
20 deletions
+26
-20
Doc/library/csv.rst
Doc/library/csv.rst
+26
-20
No files found.
Doc/library/csv.rst
View file @
9cc6249d
...
@@ -400,32 +400,36 @@ Examples
...
@@ -400,32 +400,36 @@ 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:
for row in reader:
reader = csv.reader(f)
print(row)
for row in reader:
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:
for row in reader:
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
print(row)
for row in reader:
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.writerows(someiterable)
writer = csv.writer(f)
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
will by default be decoded into unicode using the system default
will by default be decoded into unicode using the system default
encoding (see :func:`locale.getpreferredencoding`). To decode a file
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:
for row in reader:
reader = csv.reader(f)
print(row)
for row in reader:
print(row)
The same applies to writing in something other than the system default
The same applies to writing in something other than the system default
encoding: specify the encoding argument when opening the output file.
encoding: specify the encoding argument when opening the output file.
...
@@ -434,18 +438,20 @@ Registering a new dialect::
...
@@ -434,18 +438,20 @@ 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:
try:
reader = csv.reader(f)
for row in reader:
try:
print(row)
for row in reader:
except csv.Error as e:
print(row)
sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
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
And while the module doesn't directly support parsing strings, it can easily be
done::
done::
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment