python 3.4 encoding in windows 8.1

匆匆过客 提交于 2021-02-08 10:33:12

问题


I use the script mentioned in this question, to check the encoding:

import sys, locale, os
print(sys.stdout.encoding)
print(sys.stdout.isatty())
print(locale.getpreferredencoding())
print(sys.getfilesystemencoding())
print(os.environ["PYTHONIOENCODING"])
print(chr(246), chr(9786), chr(9787))

and I obtain (python 3.4, windows 8.1):

windows-1252
False
cp1252
mbcs
windows-1252
ö Traceback (most recent call last):
  File "C:/Users/.../UTF8-comprovacio.py", line 8, in <module>
    print(chr(246), chr(9786), chr(9787))
  File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u263a' in position 0: character maps to <undefined>

I'm trying to change windows 8.1 encoding (e.g. I add an environment variable called "PYTHONIOENCODING" vith utf8 value) but the result is always the same. How can I change the encoding and the value of PYTHONIOENCODING in Windows 8.1? (in fact, I have another computer, also with Windows 8.1, that shows the correct values, utf-8, but I don't know why)


回答1:


I had the same problem last week... I ended up just changing in IDE. Don't know your IDE, but if PyCharm, starting from the menu bar: File -> Settings... -> Editor -> File Encodings, then set: "IDE Encoding", "Project Encoding" and "Default encoding for properties files" ALL to UTF-8 and she now works like a charm.

Maybe yours has a similar option.



来源:https://stackoverflow.com/questions/30955589/python-3-4-encoding-in-windows-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!