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'>
用圖總結轉換方法
參考資料: