Monday, May 16, 2011

How to read csv

How to read a csv (comma separated values) file:
import csv
reader = csv.reader(open('values.csv','rb'))
for row in reader:
 print row
The script will print a list for each line in the csv file
$ python csvread.py 
['0', '0', '5', '385', '0', '209', '0', '0', '0', '2']
['0', '0', '17', '30', '0', '5', '7', '6', '0', '0']
['11', '0', '97', '468', '0', '338', '28', '0', '0', '3']
$ cat values.csv 
0,0,5,385,0,209,0,0,0,2
0,0,17,30,0,5,7,6,0,0
11,0,97,468,0,338,28,0,0,3

No comments:

Post a Comment