I'm making a script that requires me to change the encoding format to "UTF-8". I found a topic here on Stachoverflow that said i could use:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
It works great in OSX 10.8 (maybe earlier versions too), but in Windows XP and Windows 7 (probably Vista and 8 too) it disables all feedback in the interpreter. The script still runs, but i can't print anything or see if anything goes wrong.
Is there a way to patch the current code or is there an alternate way to change the encoding?
cmd.exe
doesn't use utf-8 by default? – PulpitI'm making a script that requires me to change the encoding format
... - why ? – Electroanalysissetdefaultencoding
is not a supported way to solve any problem. It will make your Python scripts incompatible with the majority of other Python users, and may lead to unexpected behavior or moji-bake. – Holidaystr
andunicode
. This could happen in lots of ways so to help you fix your script the proper way, we'd need to see your code. In general, you just have to keep track of what isstr
and what isunicode
and don't mix them willy-nilly. Usually you'd want to convert user-inputtedstr
s tounicode
, work everywhere withunicode
, and encode yourunicode
toutf-8
or whatever is appropriate only upon output. – Holidaybytes
(that is,str
s in Python2) andstr
(or, what is calledunicode
in Python2). Instead of implicitly converting between the two using theascii
encoding, Python3 will often just raise an exception. So it will pay off in the long run to know the absolute minimum needed to deal with unicode as well as some practical advice on how to deal with unicode in Python. – Holidayunicode
object and then work with that. – Extramundane