鎻掑叆鏁版嵁搴撴椂锛屾�绘槸绌猴紝鎴戝仛浜嗗浣曟搷浣滐紝灏卞ソ浜嗐�傛暟鎹槸閲囬泦杩囨潵鐨勶紝绋嬪簭鏄痝bk缂栫爜
person_sql="insert into analyst(education_id,alys_name,alys_sex,alys_img,alys_inte,person_id,alys_ctime,org_id) select * from (select (select education_id from education where education_name='"+personInfo[6].decode('gbk').encode('utf-8')+"'),'"+personInfo[1][0]+"','"+personInfo[2][0]+"','"+img+"','"+chenxin+"',convert('"+person_id+"' using gbk) as person_id,now(),(select org_id from organ where inst_code='"+inst_code+"' limit 1) org_id ) t where t.person_id not in(select person_id from analyst b where b.person_id=t.person_id);/n";
锛堣浆锛夎В鍐砋nicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 108: ordinal not in range(128)
python聽2011-03-25 11:31:05聽闃呰21聽璇勮0聽瀛楀彿锛氬ぇ涓�灏徛犺闃�
娣锋穯浜� python2 閲岃竟鐨� str 鍜� unicode 鏁版嵁绫诲瀷銆�
0.浣犻渶瑕佺殑鏄缂栫爜鐢ㄥ疄闄呯紪鐮佽�屼笉鏄� ascii
1.
瀵归渶瑕� str->unicode 鐨勪唬鐮侊紝鍙互鍦ㄥ墠杈瑰啓涓�
import sys
reload(sys)
sys.setdefaultencoding('utf8')
鎶� str 缂栫爜鐢� ascii 鏀逛负 utf8 (鎴� gb18030)
2.
python3 鍖哄垎浜� unicode str 鍜� byte arrary锛屽苟涓旈粯璁ょ紪鐮佷笉鍐嶆槸 ascii
1.聽鍦≒ython涓娇鐢ㄤ腑鏂�
鍦≒ython涓湁涓ょ榛樿鐨勫瓧绗︿覆锛歴tr鍜寀nicode銆傚湪Python涓竴瀹氳娉ㄦ剰鍖哄垎鈥淯nicode瀛楃涓测�濆拰鈥渦nicode瀵硅薄鈥濈殑鍖哄埆銆傚悗闈㈡墍鏈夌殑鈥渦nicode瀛楃涓测�濇寚鐨勯兘鏄痯ython閲岀殑鈥渦nicode瀵硅薄鈥濄��
浜嬪疄涓婂湪Python涓苟娌℃湁鈥淯nicode瀛楃涓测�濊繖鏍风殑涓滆タ锛屽彧鏈夆�渦nicode鈥濆璞°�備竴涓紶缁熸剰涔変笂鐨剈nicode瀛楃涓插畬鍏ㄥ彲浠ョ敤str瀵硅薄琛ㄧず銆傚彧鏄繖鏃跺�欏畠浠呬粎鏄竴涓瓧鑺傛祦锛岄櫎闈炶В鐮佷负unicode瀵硅薄锛屾病鏈変换浣曞疄闄呯殑鎰忎箟銆�
鎴戜滑鐢ㄢ�滃搱鍝堚�濆湪澶氫釜骞冲彴涓婃祴璇曪紝鍏朵腑鈥滃搱鈥濆搴旂殑涓嶅悓缂栫爜鏄細
1锛幝犅犅犅犅犅犅犅犅犅犅犅犅� UNICODE (UTF8-16)锛� 聽聽聽聽 C854锛�
2锛幝犅犅犅犅犅犅犅犅犅犅犅犅� UTF-8锛屄犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅� E59388锛�
3锛幝犅犅犅犅犅犅犅犅犅犅犅犅� GBK锛屄犅犅犅犅犅犅犅犅犅犅犅犅犅� B9FE銆�
1.1聽聽聽聽 Windows鎺у埗鍙�
涓嬮潰鏄湪windows鎺у埗鍙扮殑杩愯缁撴灉锛�
鍙互鐪嬪嚭鍦ㄦ帶鍒跺彴锛屼腑鏂囧瓧绗︾殑缂栫爜鏄疓BK鑰屼笉鏄疷TF-16銆傚皢瀛楃涓瞫锛圙BK缂栫爜锛変娇鐢╠ecode杩涜瑙g爜鍚庯紝鍙互寰楀埌鍚岀瓑鐨剈nicode瀵硅薄銆�
娉ㄦ剰锛氬彲浠ュ湪鎺у埗鍙版墦鍗皊s骞朵笉浠h〃瀹冨彲浠ョ洿鎺ヨ搴忓垪鍖栵紝姣斿锛�
鍚戞枃浠剁洿鎺ヨ緭鍑簊s浼氭姏鍑哄悓鏍风殑寮傚父銆傚湪澶勭悊unicode涓枃瀛楃涓茬殑鏃跺�欙紝蹇呴』棣栧厛瀵瑰畠璋冪敤encode鍑芥暟锛岃浆鎹㈡垚鍏跺畠缂栫爜杈撳嚭銆傝繖涓�鐐瑰鍚勪釜鐜閮戒竴鏍枫��
鎬荤粨锛氬湪Python涓紝鈥渟tr鈥濆璞″氨鏄竴涓瓧鑺傛暟缁勶紝鑷充簬閲岄潰鐨勫唴瀹规槸涓嶆槸涓�涓悎娉曠殑瀛楃涓诧紝浠ュ強杩欎釜瀛楃涓查噰鐢ㄤ粈涔堢紪鐮侊紙gbk, utf-8, unicode锛夐兘涓嶉噸瑕併�傝繖浜涘唴瀹归渶瑕佺敤鎴疯嚜宸辫褰曞拰鍒ゆ柇銆傝繖浜涚殑闄愬埗涔熷悓鏍烽�傜敤浜庘�渦nicode鈥濆璞°�傝璁颁綇鈥渦nicode鈥濆璞′腑鐨勫唴瀹瑰彲缁濆涓嶄竴瀹氬氨鏄悎娉曠殑unicode瀛楃涓诧紝鎴戜滑寰堝揩灏变細鐪嬪埌杩欑鎯呭喌銆�
鎬荤粨锛氬湪windows鐨勬帶鍒跺彴涓婏紝鏀寔gbk缂栫爜鐨剆tr瀵硅薄鍜寀nicode缂栫爜鐨剈nicode瀵硅薄銆�
1.2聽聽聽聽 Windows IDLE锛堝湪Shell涓婅繍琛岋級
鍦╳indows涓嬬殑IDLE涓紝杩愯鏁堟灉鍜寃indows鎺у埗鍙颁笉瀹屽叏涓�鑷达細
鍙互鐪嬪嚭锛屽浜庝笉浣跨敤鈥渦鈥濅綔鏍囪瘑鐨勫瓧绗︿覆锛孖DLE鎶婂叾涓殑涓枃瀛楃杩涜GBK缂栫爜銆備絾鏄浜庝娇鐢ㄢ�渦鈥濈殑unicode瀛楃涓诧紝IDLE灞呯劧涓�鏍锋槸鐢ㄤ簡GBK缂栫爜锛屼笉鍚岀殑鏄紝杩欐椂鍊欐瘡涓�涓瓧绗﹂兘鏄痷nicode锛堝璞★級瀛楃锛侊紒姝ゆ椂len(ss) = 4銆�
杩欐牱浜х敓浜嗕竴涓濂囩殑闂锛岀幇鍦ㄧ殑ss鏃犳硶鍦↖DLE涓甯告樉绀恒�傝�屼笖鎴戜篃娌℃湁鍔炴硶鎶妔s杞崲鎴愭甯哥殑缂栫爜锛佹瘮濡傞噰鐢ㄤ笅闈㈢殑鏂规硶锛�
杩欐湁鍙兘鏄洜涓篒DLE鏈湴鍖栧仛寰椾笉澶熷ソ锛屽涓枃鐨勬敮鎸佹湁闂銆傚缓璁湪IDLE鐨凷HELL涓紝涓嶈浣跨敤u鈥滀腑鏂団�濊繖绉嶆柟寮忥紝鍥犱负杩欐牱寰楀埌鐨勫苟涓嶆槸浣犳兂瑕佺殑涓滆タ銆�
杩欏悓鏃惰鏄嶪DLE鐨凷hell鏀寔涓ょ鏍煎紡鐨勪腑鏂囧瓧绗︿覆锛欸BK缂栫爜鐨勨�渟tr鈥濆璞★紝鍜孶NICODE缂栫爜鐨剈nicode瀵硅薄銆�
1.3聽聽聽聽 鍦↖DLE涓婅繍琛屼唬鐮�
鍦↖DLE鐨凷HELL涓婅繍琛屾枃浠讹紝寰楀埌鐨勫張鏄笉鍚岀殑缁撴灉銆傛枃浠剁殑鍐呭鏄細
鐩存帴杩愯鐨勭粨鏋滄槸锛�
姣棤鐟曠柕锛岀浉褰撲护浜烘弧鎰忋�傛垜娌℃湁璇曡繃鍏跺畠缂栫爜鐨勬枃浠舵槸鍚﹁兘姝e父杩愯锛屼絾鎯虫潵搴旇鏄笉閿欑殑銆�
鍚屾牱鐨勪唬鐮佸湪windows鐨勬帶鍒跺彴璇曟紨杩囷紝涔熸病鏈変换浣曢棶棰樸��
1.4聽聽聽聽 Windows Eclipse
鍦‥clipse涓鐞嗕腑鏂囨洿鍔犲洶闅撅紝鍥犱负鍦‥clipse涓紝缂栧啓浠g爜鍜岃繍琛屼唬鐮佸睘浜庝笉鍚岀殑绐楀彛锛岃�屼笖浠栦滑鍙互鏈変笉鍚岀殑榛樿缂栫爜銆傚浜庡涓嬩唬鐮侊細
#!/usr/bin/python
# -*- coding: utf-8 -*-
s =聽"鍝堝搱"
ss = u'鍝堝搱'
print repr(s)
print repr(ss)
print s.decode('utf-8').encode('gbk')
print ss.encode('gbk')
print s.decode('utf-8')
print ss
鍓嶅洓涓猵rint杩愯姝e父锛屾渶鍚庝袱涓猵rint閮戒細鎶涘嚭寮傚父锛�
'/xe5/x93/x88/xe5/x93/x88'
u'/u54c8/u54c8'
鍝堝搱
鍝堝搱
Traceback (most recent call last):
聽File "E:/Workspace/Eclipse/TestPython/Test/test_encoding_2.py", line 13, in
聽聽聽 print s.decode('utf-8')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
涔熷氨鏄锛孏BK缂栫爜鐨剆tr瀵硅薄鍙互姝e父鎵撳嵃锛屼絾鏄笉鑳芥墦鍗癠NICODE缂栫爜鐨剈nicode瀵硅薄銆傚湪婧愭枃浠朵笂鐐瑰嚮鈥淩un as鈥濃�淩un鈥濓紝鐒跺悗鍦ㄥ脊鍑哄璇濇涓�夋嫨鈥淐ommon鈥濓細
鍙互鐪嬪嚭Eclipse鎺у埗鍙扮殑缂虹渷缂栫爜鏂瑰紡鏄疓BK锛涙墍浠ヤ笉鏀寔UNICODE涔熷湪鎯呯悊涔嬩腑銆傚鏋滄妸鏂囦欢涓殑coding淇敼鎴怗BK锛屽垯鍙互鐩存帴鎵撳嵃GBK缂栫爜鐨剆tr瀵硅薄锛屾瘮濡俿銆�
濡傛灉鎶婃簮鏂囦欢鐨勭紪鐮佽缃垚鈥淯TF-8鈥濓紝鎶婃帶鍒跺彴鐨勭紪鐮佷篃璁剧疆鎴愨�淯TF-8鈥濓紝鎸夐亾鐞嗚鎵撳嵃鐨勬椂鍊欏簲璇ユ病鏈夐棶棰樸�備絾鏄疄楠岃〃鏄庯紝鍦ㄦ墦鍗癠TF-8缂栫爜鐨剆tr瀵硅薄鏃讹紝涓枃鐨勬渶鍚庝竴涓瓧绗︿細鏄剧ず鎴愪贡鐮侊紝鏃犳硶姝e父闃呰銆備笉杩囨垜宸茬粡寰堟弧瓒充簡锛岃嚦灏戜汉瀹舵病鏈夋姏寮傚父涓嶆槸:)
BTW: 浣跨敤鐨凟clipse鐗堟湰鏄�3.2.1銆�
1.5聽聽聽聽 浠庢枃浠惰鍙栦腑鏂�
鍦╳indow涓嬮潰鐢ㄨ浜嬫湰缂栬緫鏂囦欢鐨勬椂鍊欙紝濡傛灉淇濆瓨涓篣NICODE鎴朥TF-8锛屽垎鍒細鍦ㄦ枃浠剁殑寮�澶村姞涓婁袱涓瓧鑺傗��/xFF/xFE鈥濆拰涓変釜瀛楄妭鈥�/xEF/xBB/xBF鈥濄�傚湪璇诲彇鐨勬椂鍊欏氨鍙兘浼氶亣鍒伴棶棰橈紝浣嗘槸涓嶅悓鐨勭幆澧冨杩欏嚑涓浜庡瓧绗︾殑澶勭悊涔熶笉涓�鏍枫��
浠indows涓嬬殑鎺у埗鍙颁负渚嬶紝鐢ㄨ浜嬫湰淇濆瓨涓変釜涓嶅悓鐗堟湰鐨勨�滃搱鍝堚�濄��
>>>>闃呰鍏ㄦ枃