python3的内部的字符串是支持转码的:
str类型 支持encode()编码为字节码
bytes类型 支持decode()重新解码为字符串, 但是要知道对应的编码
>>> s = '你好,world'
>>>
>>> type(s)
<class 'str'>
>>>
>>> b = s.encode('utf-8')
>>> b
b'\xe4\xbd\xa0\xe5\xa5\xbd,world'
>>>
>>> type(b)
<class 'bytes'>
>>>
>>> b.decode('utf-8')
'你好,world'
>>>
另外,写入文件时可以设定当前编码encoding='xxx' :
with open('set.txt','r',encoding='utf-8') as f:
print(f.read())