13

I have a problem with the csv module in python.

This is the code I've written to parse my csv

def parse(data):
    data_delim = data.split("\n")
    data_list = csv.reader(data_delim)
    return data_list

The problem I am encountering is the following:

print(data_list[Enum.check_name(skill)][1])

Throws this error

_csv.reader' object is not subscriptable

I have a ghetto solution for this below, but I'd rather use something similar to the code above, does anyone have a solution to this?

i = 0
for a in data_list:
    if i == Enum.check_name(skill):
        print(a[1])
    i += 1
  • What do you intend `data_list[Enum.check_name(skill)]` to do? You can't use indexing to say "get the row with this value in the first column". – BrenBarn Aug 16 '15 at 19:00
  • I was intending it the get the value at index Enum.check_name(skill), for example I would compare it to writing data_list[1][1] to get the value of the second row in the second column. –  Aug 16 '15 at 19:03

1 Answers1

38

As the error message says, csv readers don't support indexing. The value returned by csv.reader is not a list; it's an iterator over the rows.

If you want, you could make a list of all the rows with data_list = list(csv.reader(data_delim)). Then you can index that list as you would any other.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384