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的方式进行读取写入会极大程度降低出现文本问题
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下