Python3 最重要的特性之一就是对 字符串 和 二进制字节 做了明确且严格的区分,之所以说严格,是指二者在任何情况下不能混用;
文本总是 Unicode,由字符串 str 表示;
二进制数据由 bytes 表示;
file1 = open('data.txt', 'r') out1 = file1.read() print(type(out1)) # <class 'str'> ### 以二进制方式读取 file2 = open('data.txt', 'rb') out2 = file2.read() print(type(out2)) # <class 'bytes'> # print(out1 + out2) # TypeError: must be str, not bytes
二者相互转换
b = b'hello' s = 'world' print(type(b)) # <class 'bytes'> print(type(s)) # <class 'str'> ### btyes to str # def __init__(self, value='', encoding=None, errors='strict') bs1 = str(b, encoding='utf-8') # def decode(self, *args, **kwargs) bs2 = bytes.decode(b, encoding='utf-8') print(type(bs1), type(bs2)) # <class 'str'> <class 'str'> ### str to bytes # def __init__(self, value=b'', encoding=None, errors='strict') sb1 = bytes(s, encoding='utf-8') # def encode(self, encoding='utf-8', errors='strict') sb2 = str.encode(s, encoding='utf-8') print(type(sb1), type(sb2)) # <class 'bytes'> <class 'bytes'>
用图总结转换方法
参考资料: