I tried sys.getdefaultencoding()
but unfortunately that doesn't work. It returns ascii
on my system which has the system locale set to ja_JP (i.e. the encoding should be Shift-JIS).
I'm trying to parse CJK text (on Windows). I need to read some text from keyboard, determine the system encoding, and convert it to utf8. I would often change my system locale between zh_CN (GBK encoding) and ja_JP (Shift-JIS encoding) so hard-coding the system encoding (encoding of keyboard-input text) is not an option. Any solutions?
Solved: sys.stdin.encoding
Also, for anyone trying sys.getdefaultencoding()
, it would almost never work and would always be ascii
according to https://wiki.python.org/moin/DefaultEncoding:
Its value should be 'ascii' and it is used when converting byte strings to unicode strings.
and
If you put non-ascii characters into byte string then .decode(sys.getdefaultencoding()) method will fail with UnicodeDecodeError, therefore byte strings should not contain non-ascii characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With