英文文檔: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string version of object. If object is not provided, retu ...
英文文檔:
class str
(object='') class
str
(object=b'', encoding='utf-8', errors='strict')
Return a string version of object. If object is not provided, returns the empty string. Otherwise, the behavior of str()
depends on whether encoding or errors is given, as follows.
If neither encoding nor errors is given, str(object)
returns object.__str__()
, which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a __str__()
method, then str()
falls back to returning repr(object)
.
If at least one of encoding or errors is given, object should be a bytes-like object (e.g. bytes
or bytearray
). In this case, if object is a bytes
(or bytearray
) object, then str(bytes, encoding, errors)
is equivalent to bytes.decode(encoding, errors)
. Otherwise, the bytes object underlying the buffer object is obtained before calling bytes.decode()
. See Binary Sequence Types — bytes, bytearray, memoryview and Buffer Protocol for information on buffer objects.
說明:
1. str函數功能時將對象轉換成其字元串表現形式,如果不傳入參數,將返回空字元串。
>>> str() '' >>> str(None) 'None' >>> str('abc') 'abc' >>> str(123) '123'
2. 當轉換二進位流時,可以傳入參數encoding,表示讀取位元組數組所使用的編碼格式;參數errors,表示讀取二進位的錯誤級別。(這兩個參數和open方法中的同名參數有相同取值和類似的含義,詳見Python內置函數(47)——open)。
>>> file = open('test.txt','rb') # 打開文件 >>> fileBytes = file.read() # 讀取二進位流 >>> fileBytes b'\xe6\x88\x91\xe6\x98\xaf\xe7\xac\xac1\xe8\xa1\x8c\xe6\x96\x87\xe6\x9c\xac\xef\xbc\x8c\xe6\x88\x91\xe5\xb0\x86\xe8\xa2\xab\xe6\x98\xbe\xe7\xa4\xba\xe5\x9c\xa8\xe5\xb1\x8f\xe5\xb9\x95\r\n\xe6\x88\x91\xe6\x98\xaf\xe7\xac\xac2\xe8\xa1\x8c\xe6\x96\x87\xe6\x9c\xac\xef\xbc\x8c\xe6\x88\x91\xe5\xb0\x86\xe8\xa2\xab\xe6\x98\xbe\xe7\xa4\xba\xe5\x9c\xa8\xe5\xb1\x8f\xe5\xb9\x95\r\n\xe6\x88\x91\xe6\x98\xaf\xe7\xac\xac3\xe8\xa1\x8c\xe6\x96\x87\xe6\x9c\xac\xef\xbc\x8cr\xe6\x88\x91\xe5\xb0\x86\xe8\xa2\xab\xe6\x98\xbe\xe7\xa4\xba\xe5\x9c\xa8\xe5\xb1\x8f\xe5\xb9\x95' >>> str(fileBytes) # 預設將二進位流轉換成字元串表現形式 "b'\\xe6\\x88\\x91\\xe6\\x98\\xaf\\xe7\\xac\\xac1\\xe8\\xa1\\x8c\\xe6\\x96\\x87\\xe6\\x9c\\xac\\xef\\xbc\\x8c\\xe6\\x88\\x91\\xe5\\xb0\\x86\\xe8\\xa2\\xab\\xe6\\x98\\xbe\\xe7\\xa4\\xba\\xe5\\x9c\\xa8\\xe5\\xb1\\x8f\\xe5\\xb9\\x95\\r\\n\\xe6\\x88\\x91\\xe6\\x98\\xaf\\xe7\\xac\\xac2\\xe8\\xa1\\x8c\\xe6\\x96\\x87\\xe6\\x9c\\xac\\xef\\xbc\\x8c\\xe6\\x88\\x91\\xe5\\xb0\\x86\\xe8\\xa2\\xab\\xe6\\x98\\xbe\\xe7\\xa4\\xba\\xe5\\x9c\\xa8\\xe5\\xb1\\x8f\\xe5\\xb9\\x95\\r\\n\\xe6\\x88\\x91\\xe6\\x98\\xaf\\xe7\\xac\\xac3\\xe8\\xa1\\x8c\\xe6\\x96\\x87\\xe6\\x9c\\xac\\xef\\xbc\\x8cr\\xe6\\x88\\x91\\xe5\\xb0\\x86\\xe8\\xa2\\xab\\xe6\\x98\\xbe\\xe7\\xa4\\xba\\xe5\\x9c\\xa8\\xe5\\xb1\\x8f\\xe5\\xb9\\x95'" >>> str(fileBytes,'utf-8') # 傳入encoding參數,函數將以此編碼讀取二進位流的內容 '我是第1行文本,我將被顯示在屏幕\r\n我是第2行文本,我將被顯示在屏幕\r\n我是第3行文本,r我將被顯示在屏幕' >>> str(fileBytes,'gbk') # 當傳入encoding不能解碼時,會報錯(即errors參數預設為strict) Traceback (most recent call last): File "<pyshell#46>", line 1, in <module> str(fileBytes,'gbk') UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 8: illegal multibyte sequence >>> str(fileBytes,'gbk','ignore') # 'ignore' 忽略級別,字元編碼有錯,忽略掉. '鎴戞槸絎1琛屾枃鏈錛屾垜灝嗚鏄劇ず鍦ㄥ睆騫\r\n鎴戞槸絎2琛屾枃鏈錛屾垜灝嗚鏄劇ず鍦ㄥ睆騫\r\n鎴戞槸絎3琛屾枃鏈錛宺鎴戝皢琚鏄劇ず鍦ㄥ睆騫' >>> str(fileBytes,'gbk','replace') # 'replace' 替換級別,字元編碼有錯的,替換成?. '鎴戞槸絎�1琛屾枃鏈�錛屾垜灝嗚��鏄劇ず鍦ㄥ睆騫�\r\n鎴戞槸絎�2琛屾枃鏈�錛屾垜灝嗚��鏄劇ず鍦ㄥ睆騫�\r\n鎴戞槸絎�3琛屾枃鏈�錛宺鎴戝皢琚�鏄劇ず鍦ㄥ睆騫�'