2

I have this line:

#str = u'Harsha: This has unicode character ♭.\n'

This line causes SyntaxError: Non-ASCII character '\xe2' even if it's commented. If I remove this line the error is gone. Can anyone tell me whats wrong here?
I'm using PyCharm as IDE.

freshbm
  • 5,540
  • 5
  • 46
  • 75
Harsh
  • 72
  • 1
  • 7

2 Answers2

2

You want to add the following line at the top of your source file:

# -*- coding: utf-8 -*-

This tells python what is the encoding of your source file.

Source: Working with utf-8 encoding in Python source

Community
  • 1
  • 1
Régis B.
  • 10,092
  • 6
  • 54
  • 90
0

You need to hint the proper file encoding.

As you know the character e2 is represented by binary string

1110 ...

this is ambiguos because it could be the UTF8 starting byte for a triplet, or just a Extended ASCII character (wich is what you wanted).

Python defaults to ASCII (7 bit character) that means that without giving some hint for parsing the code everythin over 7 bit will be considered ambiguos and hence lead to an error.

You should instead escape that character or if possible hint the python interpreter to do so (I don't know if it possible, I only found a proposal for that but I don't know if that is implemented already)

CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69