python內部使用的是unicode編碼,而外部會用到各種編碼,中國最常用utf-8。
python默認會認為源代碼文件是ascii編碼,ascii編碼中不存在中文,遇到中文會拋出異常。
一、聲明utf-8編碼格式
此時需要在頭部聲明utf-8編碼格式,常用聲明方法有這么3種
# coding=utf-8 # coding:utf-8 #!/usr/bin/python # -*- coding:utf-8 -*-
注意:
前兩種不能有空格。
第一行和第二行重復定義,第一行生效,第二行不生效。
在python 里認為utf8和utf-8是一種編碼格式utf-8。
不過這不是萬能的,對於有的漢字,還是偶爾會亂碼,這時候轉換成unicode對象一般就可以了
二、轉換成unicode對象
將字符串轉換成unicode對象常用的3種方法:
1、字符串前面加u
s1=u'國道圖層' print ('s1:'+s1)
u將字符串以unicode格式存儲。python頭部聲明的utf-8編碼識別字符串,然后轉換成unicode對象。
也就是說不加u就是某種編碼的字符串,這里是utf-8編碼的字符串。
如果加u就是一個unicode對象。
可以用type()函數查看數據類型。
2、unicode()
s2=unicode('國道圖層', 'utf-8')
print ('s2:'+s2)
unicode類需要傳入兩個參數,第一個是字符串參數,第二個是編碼參數,會將字符串封裝為一個unicode對象。
3、decode()
s3='國道圖層'.decode('utf-8')
print ('s3:'+s3)
decode是將普通字符串按照參數中的編碼格式進行解析,然后生成對應的unicode對象。
補充:與decode()相反的就是encode(),encode()是將一個unicode對象轉換為參數中編碼格式的普通字符。如下:
s4=unicode('國道圖層', 'utf-8').encode('utf-8')
print ('s4:'+s4)
三、代碼測試
#coding=utf-8 s='國道圖層' print ('s:'+s) print(type(s)) s1=u'國道圖層' print ('s1:'+s1) print(type(s1)) s2=unicode('國道圖層', 'utf-8') print ('s2:'+s2) print(type(s2)) s3='國道圖層'.decode('utf-8') print ('s3:'+s3) print(type(s3)) s4=unicode('國道圖層', 'utf-8').encode('utf-8') print ('s4:'+s4) print(type(s4))
運行結果:
>>> ================================ RESTART ================================ >>> s:鍥介亾鍥懼眰 <type 'str'> s1:國道圖層 <type 'unicode'> s2:國道圖層 <type 'unicode'> s3:國道圖層 <type 'unicode'> s4:鍥介亾鍥懼眰 <type 'str'>