Python2和Python3之间的str处理方式导致乱码的讲解
Python字符串问题
- 在arcpy中版本为python2.x
- 在QGIS中版本为python2.x或者python3.x
- python2和python3之间的str处理方式经常会导致乱码,故出此文
python3版本
#将str或字节并始终返回str defto_str(bytes_or_str): ifisinstance(bytes_or_str,bytes): value=bytes_or_str.decode(‘utf-8') else: value=bytes_or_str returnvalue #将str或字节并始终返回bytes defto_bytes(bytes_or_str): ifisinstance(bytes_or_str,str): value=bytes_or_str.encode(‘utf-8') else: value=bytes_or_str returnvalue
python2版本
-在python2版本中使用unicode方式
#接受str或unicode,并总是返回unicode defto_unicode(unicode_or_str): ifisinstance(unicode_or_str,str): value=unicode_or_str.decode(‘utf-8') else: value=unicode_or_str returnvalue #接受str或unicode,并总是返回str defto_str(unicode_or_str): ifisinstance(unicode_or_str,unicode): value=unicode_or_str.encode(‘utf-8') else: value=unicode_or_str returnvalue
备注
在python中不管任何版本,都是用bytes的方式进行读取写入会极大程度降低出现文本问题
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短