Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Python×Ö·û´®µÄencodeÓëdecode


Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚPythonÄÚ²¿µÄ±íʾÊÇunicode±àÂ룬Òò´Ë£¬ÔÚ×ö±àÂëת»»Ê±£¬Í¨³£ÐèÒªÒÔunicode×÷ΪÖмä±àÂ룬¼´ÏȽ«ÆäËû±àÂëµÄ×Ö·û´®½âÂ루decode£©³Éunicode£¬ÔÙ´Óunicode±àÂ루encode£©³ÉÁíÒ»ÖÖ±àÂë¡£
decodeµÄ×÷ÓÃÊǽ«ÆäËû±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂ룬Èçstr1.decode('gb2312')£¬±íʾ½«gb2312±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂë¡£
encodeµÄ×÷ÓÃÊǽ«unicode±àÂëת»»³ÉÆäËû±àÂëµÄ×Ö·û´®£¬Èçstr2.encode('gb2312')£¬±íʾ½«unicode±àÂëµÄ×Ö·û´®×ª»»³Égb2312±àÂë¡£
ÔÚijЩIDEÖУ¬×Ö·û´®µÄÊä³ö×ÜÊdzöÏÖÂÒÂ룬ÉõÖÁ´íÎó£¬ÆäʵÊÇÓÉÓÚIDEµÄ½á¹ûÊä³ö¿ØÖÆ̨×ÔÉí²»ÄÜÏÔʾ×Ö·û´®µÄ±àÂ룬¶ø²»ÊdzÌÐò±¾ÉíµÄÎÊÌâ¡£
ÈçÔÚUliPadÖÐÔËÐÐÈçÏ´úÂ룺
s=u"ÖÐÎÄ"
print s
»áÌáʾ£ºUnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)¡£ÕâÊÇÒòΪUliPadÔÚÓ¢ÎÄWindowsXP
ÉϵĿØÖÆ̨ÐÅÏ¢Êä³ö´°¿ÚÊÇ°´ÕÕascii±àÂëÊä³öµÄ£¨Ó¢ÎÄϵͳµÄĬÈϱàÂëÊÇascii£©£¬¶øÉÏÃæ´úÂëÖеÄ×Ö·û´®ÊÇUnicode±àÂëµÄ£¬ËùÒÔÊä³öʱ²úÉúÁË´íÎó¡£
½«×îºóÒ»¾ä¸ÄΪ£ºprint s.encode('gb2312')
ÔòÄÜÕýÈ·Êä³ö“ÖÐÎÄ”Á½¸ö×Ö¡£
Èô×îºóÒ»¾ä¸ÄΪ£ºprint s.encode('utf8')
ÔòÊä³ö£º\xe4\xb8\xad\xe6\x96\x87£¬ÕâÊÇ¿ØÖÆ̨ÐÅÏ¢Êä³ö´°¿Ú°´ÕÕascii±àÂëÊä³öutf8±àÂëµÄ×Ö·û´®µÄ½á¹û¡£
Updated at: 2008.11.05
ÁíÍ⣬´úÂëÖÐ×Ö·û´®µÄĬÈϱàÂëÓë´úÂëÎļþ±¾ÉíµÄ±àÂëÒ»Ö£¬È磺
s='ÖÐÎÄ'
Èç
¹ûÊÇÔÚutf8µÄÎļþÖУ¬¸Ã×Ö·û´®¾ÍÊÇutf8±àÂ룬Èç¹ûÊÇÔÚgb2312µÄÎļþÖУ¬ÔòÆä±àÂëΪgb2312¡£
ÕâÖÖÇé¿öÏ£¬Òª½øÐбàÂëת»»£¬¶¼ÐèÒªÏÈÓÃdecode·½·¨½«Æäת»»³Éunicode±àÂ룬ÔÙʹÓÃencode·½·¨½«Æäת»»³ÉÆäËû±àÂ롣ͨ³££¬ÔÚûÓÐÖ¸¶¨ÌØ
¶¨µÄ±àÂ뷽ʽʱ£¬¶¼ÊÇʹÓõÄϵͳĬÈϱàÂë´´½¨µÄ´úÂëÎļþ£¬ÔÚÕâƪÎÄÕÂ
ÖпÉÒÔ¿´µ½ÈçºÎ»ñµÃϵͳµÄĬÈϱàÂë¡£
Èç¹û×Ö·û´®ÊÇÕâÑù¶¨Ò壺
s=u'ÖÐÎÄ'
Ôò¸Ã×Ö·û´®µÄ±àÂë¾Í±»Ö¸¶¨ÎªunicodeÁË£¬¼´pythonµÄÄÚ²¿±àÂ룬¶øÓë´úÂëÎļþ±¾ÉíµÄ±àÂëÎ޹ء£Òò´Ë£¬¶ÔÓÚÕâÖÖÇé¿ö×ö±àÂëת»»£¬Ö»ÐèÒªÖ±½ÓʹÓÃencode·½·¨½«Æäת»»³ÉÖ¸¶¨±àÂë¼´¿É¡£
Èç¹ûÒ»¸ö×Ö·û´®ÒѾ­ÊÇunicodeÁË£¬ÔÙ½øÐнâÂëÔò½«³ö´í£¬Òò´Ëͨ³£Òª¶ÔÆä±àÂ뷽ʽÊÇ·ñΪunicode½øÐÐÅжϣº
isinstance(s, unicode)  #ÓÃÀ´ÅжÏÊÇ·ñΪunicode


Ïà¹ØÎĵµ£º

WINCVS 1.3ÖÐÎÄ°æÕÒ²»µ½PythonµÄÎÊÌâ½â¾ö

 Ò»°ã°²×°µÄ¶¼ÊÇPython22°æ£¬wincvs1.3ÐèÒªpython2.1°æ±¾¼°ÒÔÉÏ¡£µ«ÊÇÆô¶¯¹ý³ÌÕÒ²»µ½£¬ÍøÉÏËÑË÷µÄ·½·¨²»´óÊÊÓá£×îºó¾­¹ýÊÔÑé·¢ÏÖ£¬°ÑPython22°²×°Â·¾¶ÏµÄpython22.dll¿½±´µ½wincvsµÄ°²×°Ä¿Â¼Ï¡£ÔËÐÐwincvs£¬ok¡£¾õµÃÕâ¸ö·½·¨ºÃÓõĸø¶¥Ï£¡ ......

Python×Ö·û´®µÄencodeÓëdecode

 
Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚPythonÄÚ²¿µÄ±íʾÊÇunicode±àÂ룬Òò´Ë£¬ÔÚ×ö±àÂëת»»Ê±£¬Í¨³£ÐèÒªÒÔunicode×÷ΪÖмä±àÂ룬¼´ÏȽ«ÆäËû±àÂëµÄ×Ö·û´®½âÂ루decode£©³Éunicode£¬ÔÙ´Óunicode±àÂ루encode£©³ÉÁíÒ»ÖÖ±àÂë¡£
decodeµÄ×÷ÓÃÊǽ«ÆäËû±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂ룬Èçstr1.decode('gb2312')£¬±íʾ½«gb2312±àÂëµÄ×Ö· ......

pythonÖÐÎÄĿ¼

 ÈçÓÐһ·¾¶"e:/software/²âÊÔ.txt" 
ÐèҪͨ¹ýpyhon¶Á/д "²âÊÔ.txt"Îļþ£¬¿ÉÒÔ²ÉÓÃÏÂÃæµÄ·½·¨£º 
ipath = "e:/software/²âÊÔ.txt" 
uipath = unicode(ipath , "utf8") 
È»ºóÓÃ"uipath"¾­¹ý±àÂëºóµÄ·¾¶È¥open()¼´¿É¡£ 
ÏÂÃæÊÇÎÒÔÚÏîÄ¿ÖÐÓÃpython¶ÁдexcelµÄÒ»µãÌå»á(½â¾öÁ˶ÁÖÐÎÄÎļþà ......

²ËÄñѧPython£¨Ò»£©

   Ϊ³äʵ×Ô¼ºÄÇƽӹµÄ֪ʶ¿â£¬´Ó½ñÌ쿪ʼ£¬Ñ§Ï°PythonÓïÑÔ¡£   Ì«¸ßÉîµÄ×ÊÁÏ¿´²»¶®£¬µ±Á˱¾×î¼òµ¥µÄPython¼òÃ÷½Ì³Ì£¬ÖÐÎÄ°æchm¸ñʽµÄ¡£²»Ëµ·Ï»°ÁË£¬¿ªÊ¼½øÈëPythonµÄÊÀ½ç°É¡£ £¨Ò»£©°²×°Python    дÕâƪÎÄÕÂʱ£¬PythonµÄ×îа汾ÊÇ3.1£¬ÎÒϲ»¶ÓÃ×îеģ¬¾ÍÏÂÁËа汾µÄ¡£Ò»Â·NextÏÂÈ¥£¬ÎÒµÄC ......

Python ×Ö·û´®·½·¨Ïê½â


ÀàÐÍ
·½·¨
×¢½â
Ìî³ä
center(width[, fillchar]) ,
ljust(width[, fillchar]),
rjust(width[, fillchar]),
zfill(width),
expandtabs([tabsize])
l        
fillchar ²ÎÊýÖ¸¶¨ÁËÓÃÒÔÌî³äµÄ×Ö·û£¬Ä¬ÈÏΪ¿Õ¸ñ
l        
¹Ë ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ