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
+6.5k Golang : Gorrila set route name and get the current route name
+7.8k Golang : Simple histogram example
+15.5k Golang : How to generate QR codes?
+10.4k Golang : Byte format example
+8.9k Golang : Setting variable value with ldflags
+5.2k Golang : How to solve "too many .rsrc sections" error?
+7k Golang : Append and add item in slice
+8.7k PHP : Get coordinates latitude/longitude from string
+14.8k Golang : Get sub string example
+9.8k Golang : Natural string sorting example
+3.8k Nginx and PageSpeed build from source CentOS example
+6.7k Golang : How to execute code at certain day, hour and minute?