i am not able to execute the below line of code: The error is pointing at this line
ios_data = list(read_file_ios)
ERROR:
UnicodeDecodeError Traceback (most recent call last)
in
3 opened_file_ios = open(‘AppleStore.csv’)
4 read_file_ios = reader(opened_file_ios)
----> 5 ios_data = list(read_file_ios)
6
7F:\Python\Python37\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]
24
25 class StreamWriter(Codec,codecs.StreamWriter):UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x81 in position 2693: character maps to
This is the set of codes:
from csv import reader
opened_file_ios = open(‘AppleStore.csv’)
read_file_ios = reader(opened_file_ios)
ios_data = list(read_file_ios)def explore_ios_data(ios_dataset=ios_data, start=1, end=6, rows_and_columns=True):
ios_data_slicing = ios_dataset[start:end]
for row_1 in ios_data_slicing:
print(row_1)
print(’\n’)if rows_and_columns:
print("Number of rows: ", len(ios_dataset))
print("Number of columns: ", len(ios_dataset[0]))opened_file_google = open(‘googleplaystore.csv’)
read_file_google = reader(opened_file_google)
google_data = list(read_file_google)def explore_google_data(google_dataset=google_data, start=1, end=6, rows_and_columns=True):
google_data_slicing = google_dataset[start:end]
for row_g in google_data_slicing:
print(row_g)
print(’\n’)if rows_and_columns:
print("Number of rows: ", len(google_data))
print("Number of columns: ", len(google_data[0]))
Hi Abhi! Welcome to the community.
Next time do remember to link the mission you’re talking about, it makes it easier for other users to respond!
Let’s look at the error you’re getting:
F:\Python\Python37\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]
24
25 class StreamWriter(Codec,codecs.StreamWriter):UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x81 in position 2693: character maps to
Currently, you’re trying to open the file using CP1252
encoding, whereas it actually uses utf8
encoding. You can fix this by specifying the file’s encoding when you first open it. Simply replace open(‘AppleStore.csv’)
with open('AppleStore.csv', encoding='utf8')
.
Thanks for quick response!
I have tried this solution first as per instructions in the exercise, it didn’t work. I will try out again. I was trying out this in pycharm and its syntax prompter was confusing too.
i had actually asked this question directly to Dataquest support, then i came to know about community hence just pasted the same without mentioning the mission#.
Hmm, if that doesn’t work either, I guess you could also try:
open('AppleStore.csv', encoding='utf8', errors='ignore')
I can’t personally speak to whether anything is made different by your choice of IDE, but perhaps someone else might have input on that!
Editor or IDE has its own default encoding settings. You have to check with editor or IDE 's documentation on how to modify encoding default to use “UTF-8” instead.
Although I do not use pycharm, here is a guide to configuring default encoding settings on pycharm.
I used vi (or vim) via terminal to edit my files.
To configure “UTF-8” encoding as default in vi (or vim) editor,
Add the following lines to ~/.vimrc file:
set encoding=utf-8
Additional references on handling byte data
According to Pragmatic Unicode, these are the five unavoidable Facts of Life:
- All input and output of your program is bytes.
- The world needs more than 256 symbols to communicate text.
- Your program has to deal with both bytes and Unicode.
- A stream of bytes can’t tell you its encoding.
- Encoding specifications can be wrong.
Some good alternative discussions of Python’s Unicode support are:
- Processing Text Files in Python 3, by Nick Coghlan.
- Pragmatic Unicode, a PyCon 2012 presentation by Ned Batchelder.
thank you for response! its working
Also thank you for additional info:+1:
You are welcome.
Once again, you are welcome.
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.
Best,
Alvin.