import csv
with open('some.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
print row
python2 test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
import csv
File "/home/miruss/virtualbox/csv.py", line 4, in <module>
f = open(sys.argv[1], 'rt')
IndexError: list index out of range
def read_attributes(filename):
with open(filename, 'r') as f:
header = f.readline().strip()
attributes = [attr.strip() for attr in header.split(",")]
return attributes
def read_data(filename, attributes):
with open(filename) as f:
lines = [line.strip() for line in f.readlines()]
del lines[0] # header :)
data = []
for line in lines:
data.append(dict(zip(attributes, [datum.strip() for datum in line.split(",")])))
return data