I want to use a ‘.’ as a delimiter, but I get an “IndexError: list index out of range” error with the code below. If I remove the delimiter portion, the code works as I want it to. Any idea why this? Many thanks in advance.
import csv
mags = csv.reader(open("mags.csv","rU"), delimiter='.')
for row in mags:
print [item.lower() for item in [row[index] for index in (1, 0)]]
Perhaps there is a line that is missing the
.delimiter.To see what csv.reader is doing, add
print repr(row)just before the other print-statement. That will probably reveal a line that was parsed into only zero or one fields.If the data actually had a
.delimiter in every row, your code would work fine:It could be that your input data has commas for delimiters and that you want a different delimiter for the output. Here’s how: