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
+22.5k Golang : Read directory content with filepath.Walk()
+5.4k PHP : Hide PHP version information from curl
+5.7k Fix fatal error: evacuation not done in time problem
+5.5k Golang *File points to a file or directory ?
+19.5k Golang : Fix cannot download, $GOPATH not set error
+10.3k Golang : How to tokenize source code with text/scanner package?
+5.7k PHP : Convert CSV to JSON with YQL example
+7.4k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+36.8k Golang : Display float in 2 decimal points and rounding up or down
+12.4k Golang : Simple client-server HMAC authentication without SSL example
+14.3k Golang : Convert IP version 6 address to integer or decimal number
+7.6k Golang : Shuffle strings array