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.9k Golang : Fix go.exe is not compatible with the version of Windows you're running
+6.4k Golang : Use modern ciphers only in secure connection
+8.4k Golang : How to control fmt or log print format?
+6.4k Golang : Mapping Iban to Dunging alphabets
+4.6k Unix/Linux : How to archive and compress entire directory ?
+7.3k Golang : Qt splash screen with delay example
+11k Golang : Get remaining text such as id or filename after last segment in URL path
+43.2k Golang : Use wildcard patterns with filepath.Glob() example
+14.5k Golang : invalid character ',' looking for beginning of value
+11.4k Android Studio : Highlight ImageButton when pressed on example
+6.3k Golang : How to iterate a slice without using for loop?
+11.5k Golang : Forwarding a local port to a remote server example