掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流
python 中的encode()函數(shù)有助于將給定的字符串轉(zhuǎn)換為編碼格式。如果未指定編碼,默認(rèn)情況下將使用 UTF-8。

創(chuàng)新互聯(lián)從2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元唐山做網(wǎng)站,已為上家服務(wù),為唐山各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):028-86922220
**string.encode(encoding='UTF-8',errors='strict')** #where encodings being utf-8, ascii, etc
encode()函數(shù)接受兩個(gè)可選參數(shù)。這里的參數(shù)錯(cuò)誤有六種類(lèi)型。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 編碼 | 字符串必須編碼到的編碼類(lèi)型 | 可選擇的 |
| 錯(cuò)誤 | 編碼失敗時(shí)的響應(yīng) | 可選擇的 |
默認(rèn)情況下,函數(shù)使用 utf-8 編碼,如果出現(xiàn)任何故障,它會(huì)引發(fā)一個(gè) UnicodeDecodeError 異常。
| 投入 | 返回值 | | 線(xiàn) | 編碼字符串 |
encode()方法的示例 # unicode string
string = 'pyth?n!'
# print string
print('The original string is:', string)
# default encoding to utf-8
string_utf8 = string.encode()
# print result
print('The encoded string is:', string_utf8)
輸出:
The original string is: pyth?n!
The encoded string is: b'pyth\xc3\xb6n!' # unicode string
string = 'pyth?n!'
# print string
print('The original string is:', string)
# ignore error
print('The encoded string (with ignore) is:', string.encode("ascii", "ignore"))
# replace error
print('The encoded string (with replace) is:', string.encode("ascii", "replace"))
輸出:
The original string is: pyth?n!
The encoded string (with ignore) is: b'pythn!'
The encoded string (with replace) is: b'pyth?n!' 
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流