So you played around with Python enough to get to this error? Good. Now let’s see what happened here. When you copy something and paste it into a .py file, some of the characters may not be copied correctly. Given below is the error Python usually spews out when that happens:
SyntaxError: Non-ASCII character ‘\xe2’ in file … on line 40,
but no decoding declared …
This is caused by the replacing standard characters like apostrophe (‘) by non-standard characters like quotation mark (`) during copying. It happens quite often when you copy text from a pdf file. The difference is very subtle, but there is a huge difference as far as Python is concerned. The apostrophe is completely legal to indicate a text string, but the quotation mark is not.
Technically, it’s not exactly illegal to use any kind of characters we want. It’s just that we have to tell Python what kind of encoding we are using so that it knows what to do with these non-standard characters. To fix that error, add this line to the top of that file:
# coding: utf-8
UTF-8 is an encoding format to represent the characters in the Unicode set. It is used very widely on the web. Unicode is the industry standard for representing and handling text on many different platforms including the web, enterprise software, printing etc. UTF-8 is one of the more popular ways used for encoding this character set.
7 thoughts on “Non-ASCII Character Syntax Error In Python”
Thanks! I would give you a hug right now!
Haha … I know the pain, buddy!
I know this site gives quality dependent articles and extra data, is there any other
web page which offers these stuff in quality?
It’s hard to come by well-informed people in this particular subject, but you sound like you know what
you’re talking about! Thanks
Amazing bro.. Seriously, this error was like dragging me to hell since last hour
Damn this error. Scratched my head a long time on this. Thanks man.
That was very helpful, thanks!