TypeError: '_csv.reader' object is not subscriptable

Hey guys!

Loving data quest so far - however, I’m a bit stumped. I’m working on the first project (Guided Project: Profitable App Profiles for the App Store and Google Play Markets) and have installed Jupyter Notebook locally.

The issue is when I try to reference my AppleStore.csv I get the error as follows -

UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-14-9ed6db075c2b> in <module>
      2 from csv import reader
      3 read_file = reader(opened_file)
----> 4 apps_data = list(read_file)
      5 print(read_file[0:2])

~\Anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     25 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x9d in position 8066: character maps to

I then looked this up in the community and saw there was a solution for it by adding encoding =‘UTF-8’ when I open the csv. However, once I have added this to the open file i.e open(‘AppleStore.csv’, encoding=‘UTF-8’) - I get a different error which you can see below.

TypeError                                 Traceback (most recent call last)
<ipython-input-15-9f13999c6bbc> in <module>
      3 read_file = reader(opened_file)
      4 apps_data = list(read_file)
----> 5 print(read_file[0:2])

TypeError: ‘_csv.reader’ object is not subscriptable

How do I continue?

read_file cannot be converted into an array. The error message is due to treating an object as an array when the object is not.

That is, square brackets [] are the subscript operator. If you try to apply the subscript operator to an object that does not support it (such as not implementing __getitem__() ).

Example using csv.DictReader

The fieldnames parameter is a sequence. If fieldnames is omitted, the values in the first row of file f will be used as the fieldnames. Regardless of how the fieldnames are determined, the ordered dictionary preserves their original ordering.

import csv
with open('names.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    names = []
    for row in reader:
         print(row['first_name'], row['last_name'])

>>> Eric Idle
>>> John Cleese

Example using csv.reader

import csv
with open('example.csv') as f:
    readCSV = csv.reader(f, delimiter=',')
    colors = []
    for row in readCSV:


I was telling it to print the read file, which had not yet been converted to a list, which is exactly what you were saying an why I was getting those errors. I just needed to tell it to print apps_data instead.

I’ve got it how it should be now. Thanks so much!

1 Like

Hey @jjpyle9,

We have a solved feature that allows you the ability to mark something as the “correct” answer, which helps future students with the same question quickly find the solution they’re looking for.

Here’s an article on how to mark posts as solved - I don’t want to do this for you until I know that solution/explanation works.