Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the system default encoding in Python 2.x?

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?

like image 996
jack980517 Avatar asked Feb 06 '23 21:02

jack980517


1 Answers

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.

like image 52
jack980517 Avatar answered Apr 27 '23 05:04

jack980517