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
159267ee
Commit
159267ee
authored
Mar 13, 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
5a77f97a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
18 deletions
+21
-18
Doc/library/csv.rst
Doc/library/csv.rst
+21
-18
No files found.
Doc/library/csv.rst
View file @
159267ee
...
...
@@ -442,36 +442,39 @@ Examples
The simplest example of reading a CSV file::
import csv
reader = csv.reader(open("some.csv", "rb"))
with open('some.csv', 'rb') 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", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
with open('passwd', 'rb') 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", "wb"))
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(someiterable)
Registering a new dialect::
import csv
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
reader = csv.reader(open("passwd", "rb")
, 'unixpwd')
with open('passwd', 'rb') 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, "rb"))
filename = 'some.csv'
with open(filename, 'rb') as f:
reader = csv.reader(f)
try:
for row in reader:
print row
...
...
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