Non-ASCII Character Syntax Error In Python

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

  1. I know this site gives quality dependent articles and extra data, is there any other
    web page which offers these stuff in quality?

  2. 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

Leave a Reply to shiveshabhishek Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s