Here is a little tmp.py with a non ASCII character:
if __name__ == "__main__":
s = '?'
print(s)
Running it I get the following error:
Traceback (most recent call last):
File ".mp.py", line 3, in <module>
print(s)
File "C:Python32libencodingscp866.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character 'xdf' in position 0: character maps to <undefined>
The Python docs says:
By default, Python source files are treated as encoded in UTF-8...
My way of checking the encoding is to use Firefox (maybe someone would suggest something more obvious). I open tmp.py in Firefox and if I select View->Character Encoding->Unicode (UTF-8) it looks ok, that is the way it looks above in this question (wth ?
symbol).
If I put:
# -*- encoding: utf-8 -*-
as the first string in tmp.py it does not change anything—the error persists.
Could someone help me to figure out what am I doing wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…