Python轉(zhuǎn)碼問題的解決方法
更新時間:2008年10月07日 23:50:38 作者:
在Python中,可以對String調(diào)用decode和encode方法來實(shí)現(xiàn)轉(zhuǎn)碼。
比如,若要將某個String對象s從gbk內(nèi)碼轉(zhuǎn)換為UTF-8,可以如下操作
s.decode('gbk').encode('utf-8′)
可是,在實(shí)際開發(fā)中,我發(fā)現(xiàn),這種辦法經(jīng)常會出現(xiàn)異常:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence
這 是因?yàn)橛龅搅朔欠ㄗ址绕涫窃谀承┯肅/C++編寫的程序中,全角空格往往有多種不同的實(shí)現(xiàn)方式,比如\xa3\xa0,或者\(yùn)xa4\x57,這些 字符,看起來都是全角空格,但它們并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在轉(zhuǎn)碼的過程中出現(xiàn)了異常。
這樣的問題很讓人頭疼,因?yàn)橹灰址谐霈F(xiàn)了一個非法字符,整個字符串——有時候,就是整篇文章——就都無法轉(zhuǎn)碼。
解決辦法:
s.decode('gbk', ‘ignore').encode('utf-8′)
因?yàn)閐ecode的函數(shù)原型是decode([encoding], [errors='strict']),可以用第二個參數(shù)控制錯誤處理的策略,默認(rèn)的參數(shù)就是strict,代表遇到非法字符時拋出異常;
如果設(shè)置為ignore,則會忽略非法字符;
如果設(shè)置為replace,則會用?取代非法字符;
如果設(shè)置為xmlcharrefreplace,則使用XML的字符引用。
python文檔
decode( [encoding[, errors]])
Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding. errors may be given to set a different error handling scheme. The default is 'strict', meaning that encoding errors raise UnicodeError. Other possible values are 'ignore', 'replace' and any other name registered via codecs.register_error, see section 4.8.1.
s.decode('gbk').encode('utf-8′)
可是,在實(shí)際開發(fā)中,我發(fā)現(xiàn),這種辦法經(jīng)常會出現(xiàn)異常:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence
這 是因?yàn)橛龅搅朔欠ㄗ址绕涫窃谀承┯肅/C++編寫的程序中,全角空格往往有多種不同的實(shí)現(xiàn)方式,比如\xa3\xa0,或者\(yùn)xa4\x57,這些 字符,看起來都是全角空格,但它們并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在轉(zhuǎn)碼的過程中出現(xiàn)了異常。
這樣的問題很讓人頭疼,因?yàn)橹灰址谐霈F(xiàn)了一個非法字符,整個字符串——有時候,就是整篇文章——就都無法轉(zhuǎn)碼。
解決辦法:
s.decode('gbk', ‘ignore').encode('utf-8′)
因?yàn)閐ecode的函數(shù)原型是decode([encoding], [errors='strict']),可以用第二個參數(shù)控制錯誤處理的策略,默認(rèn)的參數(shù)就是strict,代表遇到非法字符時拋出異常;
如果設(shè)置為ignore,則會忽略非法字符;
如果設(shè)置為replace,則會用?取代非法字符;
如果設(shè)置為xmlcharrefreplace,則使用XML的字符引用。
python文檔
decode( [encoding[, errors]])
Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding. errors may be given to set a different error handling scheme. The default is 'strict', meaning that encoding errors raise UnicodeError. Other possible values are 'ignore', 'replace' and any other name registered via codecs.register_error, see section 4.8.1.
您可能感興趣的文章:
- python編碼總結(jié)(編碼類型、格式、轉(zhuǎn)碼)
- python使用chardet判斷字符串編碼的方法
- python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法
- Python中使用不同編碼讀寫txt文件詳解
- python將圖片文件轉(zhuǎn)換成base64編碼的方法
- python3編碼問題匯總
- python實(shí)現(xiàn)中文轉(zhuǎn)換url編碼的方法
- 跟老齊學(xué)Python之坑爹的字符編碼
- Python設(shè)置默認(rèn)編碼為utf8的方法
- 學(xué)習(xí)python處理python編碼問題
- 詳解Python中使用base64模塊來處理base64編碼的方法
- 簡單解決Python文件中文編碼問題
- python輕松實(shí)現(xiàn)代碼編碼格式轉(zhuǎn)換
- python自然語言編碼轉(zhuǎn)換模塊codecs介紹
- 使用python的chardet庫獲得文件編碼并修改編碼
- Python 十六進(jìn)制整數(shù)與ASCii編碼字符串相互轉(zhuǎn)換方法
- Python使用email模塊對郵件進(jìn)行編碼和解碼的實(shí)例教程
- Python處理JSON時的值報錯及編碼報錯的兩則解決實(shí)錄
- Python字符編碼轉(zhuǎn)碼之GBK,UTF8互轉(zhuǎn)
相關(guān)文章
Pycharm保存不能自動同步到遠(yuǎn)程服務(wù)器的解決方法
今天小編就為大家分享一篇Pycharm保存不能自動同步到遠(yuǎn)程服務(wù)器的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
tensorflow pb to tflite 精度下降詳解
這篇文章主要介紹了tensorflow pb to tflite 精度下降詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python使用Matplotlib實(shí)現(xiàn)Logos設(shè)計代碼
這篇文章主要介紹了Python使用Matplotlib實(shí)現(xiàn)Logos設(shè)計代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
Python 字符串與二進(jìn)制串的相互轉(zhuǎn)換示例
今天小編就為大家分享一篇Python 字符串與二進(jìn)制串的相互轉(zhuǎn)換示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

