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
+16.4k How to enable MariaDB/MySQL logs ?
+9.1k Golang : Wait and sync.WaitGroup example
+5.9k Golang : Decode XML data from RSS feed
+4.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+8.2k Golang : Scramble and unscramble text message by randomly replacing words
+6.5k Golang : Convert(cast) io.Reader type to string
+13.7k Golang : How to check if IP address is in range
+8.6k Javascript : Read/parse JSON data from HTTP response
+19.3k Golang : Convert date string to variants of time.Time type examples
+9.2k Golang : Convert file unix timestamp to UTC time example
+17.3k Golang : Check whether a network interface is up on your machine
+17.9k Golang : Get current URL example