Clean Code hochulshin.com

Python - csv

2016-04-10

python provides a very simple csv utility. Refer to the following example to understand how easily you could use it.

CSV file

The top row of a csv file contains column names for the following lines from second line.

"Name", "Year","WHO region","Country","Age"
"Hochul Shin", "1990","Asia","Korea","77"
"Bryson", "2000","North America","USA","80"
...

CSV Parser

DictReader function generates a directory structure for the csv file. Using the structure, you could navigate the csv very easily as below.

import csv

csvfile = open('test.xml', 'rb')
reader = csv.DictReader(csvfile)

for row in reader:
	print row
	print row['Name'], row['Country'], row.get('Age')

Output

{'Country': 'Korea', 'Age': '77', 'WHO region': 'Asia', 'Name': 'Hochul Shin', ' "Year"': ' "1990"'}
Hochul Shin Korea 77
{'Country': 'USA', 'Age': '80', 'WHO region': 'North America', 'Name': 'Bryson', ' "Year"': ' "2000"'}
Bryson USA 80

Comments