TypeError: '_csv.reader' object is not subscriptable

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: '_csv.reader' object is not subscriptable #

The Python "TypeError: '_csv.reader' object is not subscriptable" occurs when we try to use square brackets to access an index in a csv reader object. To solve the error, convert the csv reader object to a list by passing it to the list constructor.

typeerror csv reader object is not subscriptable

Here is an example of how the error occurs.

main.py
import csv with open('employees.csv', newline='', encoding='utf-8') as csvfile: csv_reader = csv.reader(csvfile, delimiter=' ', quotechar='|') print(type(csv_reader)) # 👉️ <class '_csv.reader'> # ⛔️ TypeError: '_csv.reader' object is not subscriptable print(csv_reader[0])

We used square brackets to try to access the element at index 0, but the _csv.reader object is not subscriptable.

We can get around this by converting the _csv.reader object to a list.

main.py
import csv with open('employees.csv', newline='', encoding='utf-8') as csvfile: # ✅ convert to list csv_reader = list(csv.reader(csvfile, delimiter=' ', quotechar='|')) # 👇️ [['first_name,last_name'], ['Alice,Smith'], ['Bob,Smith'], ['Carl,Smith']] print(csv_reader) print(csv_reader[0]) # 👉️ ['first_name,last_name'] print(csv_reader[1]) # 👉️ ['Alice,Smith']

The code sample assumes that we have an employees.csv file with the following content.

employees.csv
first_name,last_name Alice,Smith Bob,Smith Carl,Smith

List objects are subscriptable, so we can access the element at a specific index after converting the _csv.reader object to a list.

You can iterate over the lines of your csv file with a simple for loop.

main.py
import csv with open('employees.csv', newline='', encoding='utf-8') as csvfile: csv_reader = csv.reader(csvfile, delimiter=' ', quotechar='|') for row in csv_reader: print(', '.join(row))

Note that if you try to access a list index that is out of bounds, you would get an error. You can use a try/except statement if you need to handle that.

main.py
import csv with open('employees.csv', newline='', encoding='utf-8') as csvfile: csv_reader = list(csv.reader(csvfile, delimiter=' ', quotechar='|')) try: print(csv_reader[100]) except IndexError: print('index out of bounds') # 👉️ this runs

The example catches the IndexError that is thrown if the index is out of bounds.

The list constructor takes an iterable, such as a _csv.reader object, and converts it to a list.

The list constructor builds a list whose items are the same and in the same order as the iterable's items.

The list constructor takes an iterable that may be a sequence, a container that supports iteration or an iterator object.

Conclusion #

The Python "TypeError: '_csv.reader' object is not subscriptable" occurs when we try to use square brackets to access an index in a csv reader object. To solve the error, convert the csv reader object to a list by passing it to the list constructor.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.