0

Have a default encoding of 'cp1251'-how can it be changed to UTF-8 by default in Python3? Because the function sys.setdefaultencoding() is not working

Keithx
  • 2,994
  • 15
  • 42
  • 71
  • why do you think you need to do that? – Chad S. Nov 06 '15 at 17:19
  • cause I' m using urllib.request which gives me wrong characters when parsing cyrillic (\xd0\x9e \xd0\x9a\xd0) – Keithx Nov 06 '15 at 17:22
  • 1
    So encode/decode it properly.. Also see [this](https://anonbadger.wordpress.com/2015/06/16/why-sys-setdefaultencoding-will-break-code/) – Chad S. Nov 06 '15 at 17:24
  • http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem - Try using Requests library which automatically decodes the request to Unicode strings – Alastair McCormack Nov 12 '15 at 06:37

1 Answers1

1

Python3's str is aways in unicode. If you are working with bytearray then

mystring = b'my cp1251 byte array'.decode('cp1251')

You can keep it as a str or put it into utf-8 byte array:

my_utf_8_bytearray =  mystring.encode()
Muposat
  • 1,476
  • 1
  • 11
  • 24