Cannot open and read "AppleStore.csv"

Screen Link: https://app.dataquest.io/m/312/lists-and-for-loops/7/opening-a-file

My Code:

from csv import reader
opened_file = open('AppleStore.csv')
read_file = reader(opened_file)
apps_data = list(read_file)

print(len(apps_data))
print(apps_data[0])
print(apps_data[1:3])

What I expected to happen:
Show the length of the AppleStore table
Show the column names in the first row
Show the second and the third row

What actually happened:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-7d1dd4981329> in <module>
      5 
      6 print(len(apps_data))
----> 7 print(apps_data[0])
      8 print(apps_data[1:3])

IndexError: list index out of range

I think it was unable to read the AppleStore.csv file properly and only produce an empty set. Being frustrated, I literally copy/paste the answer and it still would not work. I tried it on a local machine and it does work as expected. Does this issue happen often in DataQuest? I’m still trying DataQuest out and I’m hesitated to pay for it now.

1 Like

Hi @leung.chi.hang.eric,

Try specifying encoding when opening the file:

from csv import reader
opened_file = open('AppleStore.csv', encoding='utf-8')
read_file = reader(opened_file)
apps_data = list(read_file)

print(len(apps_data))
print(apps_data[0])
print(apps_data[1:3])
2 Likes

Thank you Elena! It does work now. I’m not sure why it wasn’t included in the answer key.

2 Likes

By the way, when I was doing that task, it worked also without specifying the encoding. It seems that the task was changed but the solution was not updated :thinking: Well, anyway, that’s good that we resolved it!

1 Like