Python涓枃鍏ㄦ敾鐣� 涓枃涔辩爜 杈撳嚭涓枃涔辩爜

鎻掑叆鏁版嵁搴撴椂锛屾�绘槸绌猴紝鎴戝仛浜嗗浣曟搷浣滐紝灏卞ソ浜嗐�傛暟鎹槸閲囬泦杩囨潵鐨勶紝绋嬪簭鏄痝bk缂栫爜

Python涓枃鍏ㄦ敾鐣� 涓枃涔辩爜 杈撳嚭涓枃涔辩爜_第1张图片

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涓嬬殑鎺у埗鍙颁负渚嬶紝鐢ㄨ浜嬫湰淇濆瓨涓変釜涓嶅悓鐗堟湰鐨勨�滃搱鍝堚�濄��

>>>>闃呰鍏ㄦ枃

你可能感兴趣的:(Python涓枃鍏ㄦ敾鐣� 涓枃涔辩爜 杈撳嚭涓枃涔辩爜)