pythonencodingutf-8cp1251

Changing encoding python 3


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


Solution

  • 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()