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
+18.6k Golang : convert int to string
+5.1k Golang : Convert lines of string into list for delete and insert operation
+11.7k Golang : Verify Linux user password again before executing a program example
+10.1k Golang : Text file editor (accept input from screen and save to file)
+18.8k Golang : Padding data for encryption and un-padding data for decryption
+12.5k Golang : Exit, terminating or aborting a program
+17.7k Golang : Iterate linked list example
+22k Golang : Match strings by wildcard patterns with filepath.Match() function
+8.4k Android Studio : Import third-party library or package into Gradle Scripts
+7.7k Golang : Scan files for certain pattern and rename part of the files
+13.5k Golang : Query string with space symbol %20 in between
+15.9k Golang : Get sub string example