Python中文編碼問題是Coder時常碰到的煩心問題
Python一般採用Ascii,Unicode編碼,但是世界上各個國家語言存在各種編碼,比如中國的gbk,gb2312等。
首先,一般情況,python默認會認爲源代碼文件是ascii編碼
使用unicode對象的話,除了這樣使用u標記,還可以使用unicode類以及字符串的encode和decode方法。
unicode類的構造函數接受一個字符串參數和一個編碼參數,將字符串封裝爲一個unicode,比如在這裏,由於我們用的是utf-8編碼,所以unicode中的編碼參數使用’utf-8′將字符封裝爲unicode對象,然後正確輸出到控制檯:
Python2.7+WIN10環境下:
site='浙江大學'
input.send_keys(site)
會出錯,產生告警:UnicodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 0: unexpected end of data
site='浙江大學'
s1 = unicode(site, 'utf-8') #正常
input.send_keys(s1)