简体   繁体   English

更改编码python 3

[英]Changing encoding python 3

Have a default encoding of 'cp1251' -how can it be changed to UTF-8 by default in Python3? 默认编码为'cp1251' UTF-8在Python3中如何默认将其更改为UTF-8 Because the function sys.setdefaultencoding() is not working 因为函数sys.setdefaultencoding()不起作用

Python3's str is aways in unicode. Python3的str不在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: 您可以将其保留为str或放入utf-8字节数组中:

my_utf_8_bytearray =  mystring.encode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM