£¨×ª£©Python×Ö·û´®µÄencodeÓëdecode isinstance
Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚ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
ÒÔÉÏתÔØ×Ô£ºhttp://ipie.blogbus.com/logs/19379694.html
£¨×ª£©PythonµÄÀàÐÍÅжϣºisinstance
Python¿ÉÒԵõ½Ò»¸ö¶ÔÏóµÄÀàÐÍ £¬ÀûÓÃtypeº¯
Ïà¹ØÎĵµ£º
¶à¸öÖ´ÐÐÏ߳̾³£Òª¹²ÏíÊý¾Ý£¬Èç¹û½ö½ö¶ÁÈ¡¹²ÏíÊý¾Ý»¹ºÃ£¬µ«ÊÇÈç¹û¶à¸öÏß³ÌÒªÐ޸Ĺ²ÏíÊý¾ÝµÄ»°¾Í¿ÉÄܳöÏÖÎÞ·¨Ô¤ÁϵĽá¹û¡£
¼ÙÈçÁ½¸öÏ̶߳ÔÏót1ºÍt2¶¼Òª¶ÔÊýÖµnum=0½øÐÐÔö1ÔËË㣬ÄÇôt1ºÍt2¶¼¸÷¶ÔnumÐÞ¸Ä10´ÎµÄ»°£¬ÄÇônum×îÖյĽá¹ûÓ¦¸ÃΪ20¡£µ«ÊÇÈç¹ûµ±t1È¡µÃnumµÄֵʱ£¨¼ÙÈç´ËʱnumΪ0£©£¬ÏµÍ³°Ñt1µ ......
from: http://www.cnblogs.com/dahuzizyd/archive/2005/03/01/111006.html
pythonÖ§³ÖÃæÏò¶ÔÏóµÄ±à³Ì·ç¸ñ£¬ÕâÀïÖ÷Ҫ˵˵pythonÖеĶà¼Ì³Ð£º
ÏÂÃæµÄ´úÂëʹÓÃpython2.4£¬°²×°ºóʹÓÃidleµÄIDE¿ª·¢»·¾³£¨ËµÊÇIDE £¬±ÈÆðdelphi,VS.netµÈ¼òµ¥µÃÌ«¶àÁË)
´ÓFile-New²Ëµ¥½¨Á¢Ò»¸ö.pyÎļþ£¬Ð´ÏÂÃæµÄ´úÂ룺
class SuperCl ......
×î½ü¶Ôpython²úÉúÁËÐËȤ£¬ÓÚÊÇ´ÓÍøÉÏÏÂÔØÁË»ù±¾PYTHONµÄÊéºÍÎĵµ£¬¿ªÊ¼ÁËPYTHONµÄѧϰ£¬·¢ÏÖPYTHONÖеÄlist¶ÔÏóµÄ¹¦ÄÜʵÔÚÊǷdz£Ç¿´ó£¬±à³ÌÆðÀ´±ÈÆäËûµÄ³ÌÐòÓïÑÔ¶ÔÁбíµÄ²Ù×÷Òª·½±ãµÄ¶à¡£
ÔÚpythonÖж¨ÒåÒ»¸öÁбíÖ»ÐèÒªÈçÏÂÓï¾ä
li = ["a","b","c","d"]
listÓÐÐí¶àµÄº¯Êý¿ÉÒÔÓÃÀ´½øÐжÔÁбíµÄ²Ù×÷£¬Èçextend,append,inse ......
1. Basic
²Î¿¼¡¶PythonÕýÔò±í´ïʽ²Ù×÷Ö¸ÄÏ¡·
Ä£¿ére£¬perl·ç¸ñµÄÕýÔò±í´ïʽ
regex²¢²»Äܽâ¾öËùÓеÄÎÊÌ⣬ÓÐʱºò»¹ÊÇÐèÒª´úÂë
regex»ùÓÚÈ·¶¨ÐԺͷÇÈ·¶¨ÐÔÓÐÏÞ×Ô¶¯»ú
2. ×Ö·ûÆ¥Åä(ÑÐò½¥½ø)
Ôª×Ö·û
. ^ $ * + ? { [ ] \ | ( )
1) "[" ºÍ "]"³£ÓÃÀ´Ö¸¶¨Ò»¸ö×Ö·ûÀà±ð£¬Ëùν×Ö·ûÀà±ð¾ÍÊÇÄãÏëÆ¥ÅäµÄÒ»¸ö×Ö·û¼¯¡£Èç[ ......
´ÓMoteLab·µ»ØµÄ´®¿ÚÊý¾Ý£¬°üº¬messages.pickleÎļþÕâÊÇMoteLabϵͳÖд®¿ÚÊÕ¼¯Êý¾ÝµÄ×ܺͣ¬µ«ÊÇÕâЩÊý¾ÝÐèÒª½âÎöºó²ÅÄܽøÐзÖÎö¡£ÏÂÃæµÄ´úÂë¾ÍÊÇÔÚpython»·¾³ÏÂÌáÈ¡messageÓÐЧÊý¾ÝµÄ´úÂë¡£
ʹÓÃÃüÁî python TestOutput.py messages.pickle
Éú³ÉµÄtest.log¾ÍÊÇ»ñµÃµÄÓÐЧÊý¾Ý
·µ»ØÊý¾ÝµÄʾÀý
1252985727.66 recei ......