Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
Don't really know why Python couldn't handle non-ASCII characters in the source code well by default. Encounter a syntax error in while trying to print out some non-ASCII characters ( Chinese ) with Python.
chinese.py
print " 我爱你"
> python chinese.py
The interpreter throws out this error message :
File "utf8.py", line 1 SyntaxError: Non-ASCII character '\xe6' in file utf8.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
To fix, this problem. Include this line at the beginning of the your Python source code.
# - *- coding: utf- 8 - *-
After modification :
# - *- coding: utf- 8 - *-
print " 我爱你"
and now > python chinese.py
should print out :
我爱你
Hope this helps!
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+17.9k How to enable MariaDB/MySQL logs ?
+11k Golang : How to transmit update file to client by HTTP request example
+22.5k Golang : Convert seconds to minutes and remainder seconds
+16.6k Golang : File path independent of Operating System
+17.2k Golang : Get input from keyboard
+7.9k Gogland : Where to put source code files in package directory for rookie
+4.5k Linux/MacOSX : Search and delete files by extension
+18.5k Golang : How to get hour, minute, second from time?
+6.3k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+9.5k Golang : How to get username from email address
+4.7k JavaScript : Rounding number to decimal formats to display currency
+34.1k Golang : Call a function after some delay(time.Sleep and Tick)