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.9k Web : How to see your website from different countries?
+5k Golang : PGX CopyFrom to insert rows into Postgres database
+16.9k Golang : How to save log messages to file?
+25.2k Golang : Convert uint value to string type
+11.6k Golang : How to detect a server/machine network interface capabilities?
+32.6k Golang : Regular Expression for alphanumeric and underscore
+13.4k Android Studio : Password input and reveal password example
+8.2k Golang : Number guessing game with user input verification example
+25.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+14.1k Golang : Fix image: unknown format error
+4.2k Javascript : How to show different content with noscript?
+5.1k Golang : Print instead of building pyramids