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
+9.1k Golang : How to capture return values from goroutines?
+30.4k Golang : How to redirect to new page with net/http?
+8.3k Swift : Convert (cast) Character to Integer?
+6.6k Golang : Spell checking with ispell example
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+9.4k Golang : Find the length of big.Int variable example
+10.9k Golang : Command line file upload program to server example
+6.4k Golang : Test input string for unicode example
+7.5k Golang : How to stop user from directly running an executable file?
+16.1k Golang : Generate universally unique identifier(UUID) example
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+10.6k Golang : Flip coin example