(17)字符编码
* 編碼概述
? python2默認編碼是ascii
?
?
* 中文處理
??? >>> name ="中國"
??? >>> name
??? Out[265]: '\xe4\xb8\xad\xe5\x9b\xbd'?
??? 看到一些讀不懂的代碼
???
??? 文件開頭加上
??? # -*- coding: utf-8 -*-
??? 或
??? # coding:utf-8
???
??? 遇到字符串
??? unicode_str = unicode('中文', encoding='utf-8')
??? print unicode_str.encode('utf-8')
???
??? 遇到文件操作
??? import codecs
??? codecs.open('filename', encoding='utf8')
???
??? sys.getdefaultencoding() 得到python解析器的編碼
???
??? 改為解析器的編碼:
???
??? 寫在程序入口:
??? import sys
??? reload(sys)
??? sys.setdefaultencoding('utf8')
???
??? 另一個方案是在python的Lib\site-packages文件夾下新建一個sitecustomize.py,內容為: Python代碼
??? # encoding=utf8
??? import sys??
??? reload(sys)
??? sys.setdefaultencoding('utf8')?
轉載于:https://www.cnblogs.com/toby2chen/p/5239550.html
總結
- 上一篇: Andriod动态布局
- 下一篇: linux小白-基础命令-cd