Python轉碼問題的解決方法
更新時間:2008年10月07日 23:50:38 作者:
在Python中,可以對String調用decode和encode方法來實現(xiàn)轉碼。
比如,若要將某個String對象s從gbk內碼轉換為UTF-8,可以如下操作
s.decode('gbk').encode('utf-8′)
可是,在實際開發(fā)中,我發(fā)現(xiàn),這種辦法經(jīng)常會出現(xiàn)異常:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence
這 是因為遇到了非法字符——尤其是在某些用C/C++編寫的程序中,全角空格往往有多種不同的實現(xiàn)方式,比如\xa3\xa0,或者\xa4\x57,這些 字符,看起來都是全角空格,但它們并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在轉碼的過程中出現(xiàn)了異常。
這樣的問題很讓人頭疼,因為只要字符串中出現(xiàn)了一個非法字符,整個字符串——有時候,就是整篇文章——就都無法轉碼。
解決辦法:
s.decode('gbk', ‘ignore').encode('utf-8′)
因為decode的函數(shù)原型是decode([encoding], [errors='strict']),可以用第二個參數(shù)控制錯誤處理的策略,默認的參數(shù)就是strict,代表遇到非法字符時拋出異常;
如果設置為ignore,則會忽略非法字符;
如果設置為replace,則會用?取代非法字符;
如果設置為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′)
可是,在實際開發(fā)中,我發(fā)現(xiàn),這種辦法經(jīng)常會出現(xiàn)異常:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence
這 是因為遇到了非法字符——尤其是在某些用C/C++編寫的程序中,全角空格往往有多種不同的實現(xiàn)方式,比如\xa3\xa0,或者\xa4\x57,這些 字符,看起來都是全角空格,但它們并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在轉碼的過程中出現(xiàn)了異常。
這樣的問題很讓人頭疼,因為只要字符串中出現(xiàn)了一個非法字符,整個字符串——有時候,就是整篇文章——就都無法轉碼。
解決辦法:
s.decode('gbk', ‘ignore').encode('utf-8′)
因為decode的函數(shù)原型是decode([encoding], [errors='strict']),可以用第二個參數(shù)控制錯誤處理的策略,默認的參數(shù)就是strict,代表遇到非法字符時拋出異常;
如果設置為ignore,則會忽略非法字符;
如果設置為replace,則會用?取代非法字符;
如果設置為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編碼總結(編碼類型、格式、轉碼)
- python使用chardet判斷字符串編碼的方法
- python實現(xiàn)unicode轉中文及轉換默認編碼的方法
- Python中使用不同編碼讀寫txt文件詳解
- python將圖片文件轉換成base64編碼的方法
- python3編碼問題匯總
- python實現(xiàn)中文轉換url編碼的方法
- 跟老齊學Python之坑爹的字符編碼
- Python設置默認編碼為utf8的方法
- 學習python處理python編碼問題
- 詳解Python中使用base64模塊來處理base64編碼的方法
- 簡單解決Python文件中文編碼問題
- python輕松實現(xiàn)代碼編碼格式轉換
- python自然語言編碼轉換模塊codecs介紹
- 使用python的chardet庫獲得文件編碼并修改編碼
- Python 十六進制整數(shù)與ASCii編碼字符串相互轉換方法
- Python使用email模塊對郵件進行編碼和解碼的實例教程
- Python處理JSON時的值報錯及編碼報錯的兩則解決實錄
- Python字符編碼轉碼之GBK,UTF8互轉
相關文章
tensorflow pb to tflite 精度下降詳解
這篇文章主要介紹了tensorflow pb to tflite 精度下降詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python使用Matplotlib實現(xiàn)Logos設計代碼
這篇文章主要介紹了Python使用Matplotlib實現(xiàn)Logos設計代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12

