Reading, Writing and Processing CSV Files

Read a CSV (line-by-line) in Python

The following code reads from a CSV line-by-line (streaming). Because the entire file is
not loaded into RAM, the file can be of any length.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Read a CSV file in Python, libe-by-line, by Jeff Heaton (http://www.jeffheaton.com/tutorials/)
import codecs
import csv

FILENAME = "iris.csv"
ENCODING = 'utf-8'

with codecs.open(FILENAME, "r", ENCODING) as fp:
reader = csv.reader(fp)

# read CSV headers
headers = next(reader)
print(headers)

# read rest of file
for row in reader:
# Print each row
print(row)

# Print individual fields of the row
# print("{},{},{},{} = {}".format(row[0],row[1],row[2],row[3],row[4]))

Write a CSV (line-by-line) in Python

The following code writes a CSV line-by-line (streaming). Because the entire file is
not in RAM, the file can be of any length.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Write a CSV file in Python, libe-by-line, by Jeff Heaton (http://www.jeffheaton.com/tutorials/)
import codecs
import csv

FILENAME = "test.csv"
ENCODING = 'utf-8'

with codecs.open(FILENAME, "w", ENCODING) as fp:
writer = csv.writer(fp)

# read rest of file
for i in range(10):
row = [10*i, 20*i, 30*i]
writer.writerow(row)