這篇文章主要介紹了Python2和Python3之間的str處理方式導致亂碼怎么辦,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Python字符串問題
在arcpy中版本為 python2.x
在QGIS中版本為 python2.x 或者 python3.x
python2 和python3 之間的str處理方式經常會導致亂碼,故出此文
python3版本
# 將str或字節并始終返回str def to_str(bytes_or_str): if isinstance(bytes_or_str, bytes): value = bytes_or_str.decode(‘utf-8') else: value = bytes_or_str return value # 將str或字節并始終返回bytes def to_bytes(bytes_or_str): if isinstance(bytes_or_str, str): value = bytes_or_str.encode(‘utf-8') else: value = bytes_or_str return value
python2版本
- 在python2版本中使用unicode方式
# 接受str或unicode,并總是返回unicode def to_unicode(unicode_or_str): if isinstance(unicode_or_str, str): value = unicode_or_str.decode(‘utf-8') else: value = unicode_or_str return value # 接受str或unicode,并總是返回str def to_str(unicode_or_str): if isinstance(unicode_or_str, unicode): value = unicode_or_str.encode(‘utf-8') else: value = unicode_or_str return value
備注
在python中不管任何版本,都是用 bytes的方式進行讀取 寫入會極大程度降低出現文本問題
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Python2和Python3之間的str處理方式導致亂碼怎么辦”這篇文章對大家有幫助,同時也希望大家多多支持創新互聯,關注創新互聯行業資訊頻道,更多相關知識等著你來學習!
當前題目:Python2和Python3之間的str處理方式導致亂碼怎么辦-創新互聯
URL地址:http://www.yijiale78.com/article30/dppopo.html
成都網站建設公司_創新互聯,為您提供ChatGPT、網站設計公司、網站策劃、做網站、靜態網站、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯