python3與python2的字串編碼問題
阿新 • • 發佈:2018-12-27
Python3和Python2字串編碼採用不同的方式,下面分為幾部分進行比較。
1、檢視Python版本
import sys
__author__ = "author"
print(sys.version_info) #字典方式顯示
print(sys.version)
python3.6.0:
python2.7.11
2、檢視Python預設編碼方式
print(sys.getdefaultencoding()) #python3
print sys.getdefaultencoding() #python2
輸出結果為python3的為utf-8,Python2的為ascii。
3、Python3與Python2中的字串編碼區別
python3中包含兩種方式,一種為bytes,一種為str。Python2中一種為unicode,一種為bytes
其實bytes為二進位制方式,例如字串b”hello”就為bytes模式,Python3採用8位模式,python2中採用7位模式。
Python3對於編碼和解碼字元尤其嚴格,bytes與str模式是不同的型別,他們比較的結果是False,而在Python2中他們比較的結果就是True。如下所示:
Python3: Python2:
從結果中也可以看出來。要想可以判斷字元格式並且可以得到想要的格式可以編寫函式進行判斷,利用isinstance函式:
#python3
def Get_Str(str_or_bytes):
if isinstance(str_or_bytes, bytes):
VALUE = str_or_bytes.decode("utf-8")
else:
VALUE = str_or_bytes
return VALUE
def Get_Bytes(str_or_bytes):
if isinstance(str_or_bytes, str):
VALUE = str_or_bytes.encode("utf-8")
else :
VALUE = str_or_bytes
return VALUE
#測試
str1 = "abc"
str2 = b"abc"
print(Get_Bytes(str1))
print(Get_Str(str2))
#! _*_encoding=utf-8_*_
#python2
def Get_Unicode(str_or_bytes):
if isinstance(str_or_bytes, bytes):
VALUE = str_or_bytes.decode("utf-8")
else:
VALUE = str_or_bytes
return VALUE
def Get_Str(str_or_bytes):
if isinstance(str_or_bytes, str):
VALUE = str_or_bytes.encode("utf-8")
else:
VALUE = str_or_bytes
return VALUE
#測試
str1 = "abc"
str2 = b"abc"
print Get_Unicode(str2)
print Get_Str(str1)
4、在Python3中bytes模式的字串不支援%s格式化輸出,python2支援。
5、輸出到檔案,兩種方式也不同,Python3不支援bytes模式直接輸出,需要以二進位制模式輸出,Python2支援。
#python3
"""
with open("test.text", "w+") as f:
f.write("Welcome to China") #TypeError 錯誤
"""
with open("text.text", "wb+") as f:
f.write(b"Welcome to China") #正確
#python2
with open("test.text", 'a+') as f:
f.write(b"Welcome to China\n") #正確
with open("test.text", "ab+") as f:
f.write(b"Welcome to China") #正確
6、encode 與 decode應該一一對應,如下程式碼所示:
Str = "Welcome to China"
print(s.encode("gbk"))
print(s.encode("utf-8"))
print(s.encode("utf-8").decode("utf-8")
對於熱愛Python的愛好者,在Python3中編碼和解碼尤為重要。