I am trying to read the rows of a csv file. My file looks like this
Col 1, Col 2, Col3
row11, row12, row13
row21, row22, row23
row31, row32, row33
...
I use the following command to read the rows
with open('~/data.csv') as f:
r = csv.DictReader(f)
for i in range(5):
print(list(r)[i])
The output prints the first row, but then it give the out of index error right after.
IndexError Traceback (most recent call last)
<ipython-input-15-efcc4f8c760d> in <module>()
2 r = csv.DictReader(f)
3 for i in range(5):
----> 4 print(list(r)[i])
IndexError: list index out of range
I'm guessing I'm making a silly mistake somewhere, but can't spot it. Any ideas on what I am doing wrong and how to fix it?
EDIT: This is the output of print(list(r))
:
[{'Col 1': 'row11', ' Col3': ' row13', ' Col 2': ' row12'}, {'Col 1': 'row21', ' Col3': ' row23', ' Col 2': ' row22'}, {'Col 1': 'row31', ' Col3': ' row33', ' Col 2': ' row32'}, {'Col 1': 'row41', ' Col3': ' row43', ' Col 2': ' row42'}, {'Col 1': 'row51', ' Col3': ' row53', ' Col 2': ' row52'}, {'Col 1': 'row61', ' Col3': ' row63', ' Col 2': ' row62'}, {'Col 1': 'row71', ' Col3': ' row73', ' Col 2': ' row72'}, {'Col 1': 'row81', ' Col3': ' row83', ' Col 2': ' row82'}, {'Col 1': 'row91', ' Col3': ' row93', ' Col 2': ' row92'}, {'Col 1': 'row101', ' Col3': ' row103', ' Col 2': ' row102'}]