86
322
Chapter 14
Reading Data from Reader Objects in a for Loop
For large CSV files, you’ll want to use the
Reader
object in a
for
loop. This
avoids loading the entire file into memory at once. For example, enter the
following into the interactive shell:
>>> import csv
>>> exampleFile = open('example.csv')
>>> exampleReader = csv.reader(exampleFile)
>>> for row in exampleReader:
print('Row #' + str(exampleReader.line_num) + ' ' + str(row))
Row #1 ['4/5/2015 13:34', 'Apples', '73']
Row #2 ['4/5/2015 3:41', 'Cherries', '85']
Row #3 ['4/6/2015 12:46', 'Pears', '14']
Row #4 ['4/8/2015 8:59', 'Oranges', '52']
Row #5 ['4/10/2015 2:07', 'Apples', '152']
Row #6 ['4/10/2015 18:10', 'Bananas', '23']
Row #7 ['4/10/2015 2:40', 'Strawberries', '98']
After you import the
csv
module and make a
Reader
object from the
CSV file, you can loop through the rows in the
Reader
object. Each row is
a list of values, with each value representing a cell.
The
print()
function call prints the number of the current row and the
contents of the row. To get the row number, use the
Reader
object’s
line_num
variable, which contains the number of the current line.
The
Reader
object can be looped over only once. To reread the CSV file,
you must call
csv.reader
to create a
Reader
object.
Writer Objects
A
Writer
object lets you write data to a CSV file. To create a
Writer
object, you
use the
csv.writer()
function. Enter the following into the interactive shell:
>>> import csv
u >>> outputFile = open('output.csv', 'w', newline='')
v >>> outputWriter = csv.writer(outputFile)
>>> outputWriter.writerow(['spam', 'eggs', 'bacon', 'ham'])
21
>>> outputWriter.writerow(['Hello, world!', 'eggs', 'bacon', 'ham'])
32
>>> outputWriter.writerow([1, 2, 3.141592, 4])
16
>>> outputFile.close()
First, call
open()
and pass it
'w'
to open a file in write mode u. This will
create the object you can then pass to
csv.writer()
v to create a
Writer
object.
On Windows, you’ll also need to pass a blank string for the
open()
func-
tion’s
newline
keyword argument. For technical reasons beyond the scope
of this book, if you forget to set the
newline
argument, the rows in output.csv
will be double-spaced, as shown in Figure 14-1.