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

[ת]Python: python±àÂëÎÊÌâ

ÕâÊÇÒ»¸öÎÒÃÇÔÚ´¦ÀíÖÐÎÄʱ, ¾­³£Óöµ½µÄÎÊÌâ.
pythonÀïÃæ»ù±¾ÉÏÒª¿¼ÂÇÈýÖÖ±àÂë¸ñʽ
1 Ô´Îļþ±àÂë
ÔÚÎļþÍ·²¿Ê¹ÓÃcodingÉùÃ÷¡£¸æËßpython½âÊÍÆ÷¸Ã´úÂëÎļþËùʹÓõÄ×Ö·û¼¯¡£
#/usr/bin/python
#coding: utf8
2 ÄÚ²¿±àÂë
´úÂëÎļþÖеÄ×Ö·û´®£¬¾­¹ýdecodeÒԺ󣬱»×ª»»ÎªÍ³Ò»µÄunicode¸ñʽµÄÄÚ²¿Êý¾Ý£¬ÀàËÆÓÚu'*'¡£unicodeÊý¾Ý¿ÉÒÔʹÓÃencodeº¯Êý£¬ÔÙ×ÔÓÉת»»ÎªÆäËû¸ñʽµÄÊý¾Ý£¬Ï൱ÓÚÒ»¸öͳһµÄƽ̨¡£
Ö±½ÓÊäÈëunicodeÊý¾Ý
>>> u'ÄãºÃ'
u'\u4f60\u597d'
½«unicodeÊý¾Ýת»»Îªgb2312¸ñʽ
>>> u'ÄãºÃ'.encode('gb2312')
'\xc4\xe3\xba\xc3'
½«ÊäÈëµÄgb2312¸ñʽµÄÊý¾Ý½âÂëΪunicode
>>> 'ÄãºÃ'.decode('gb2312')
u'\u4f60\u597d'
ÊäÈëÊý¾ÝµÄ¸ñʽȡ¾öÓÚËùÓÃshellÖն˵ıàÂëÉèÖ㬱¾ÀýÖÐΪzh_CN
[root@chenzheng python]# echo $LANG
zh_CN
½âÂëͬʱת»»Îªutf8
>>> 'ÄãºÃ'.decode('gb2312').encode('utf8')
'\xe4\xbd\xa0\xe5\xa5\xbd'
3 ÍⲿÊäÈëµÄ±àÂë
ÆäʵÕâ¸öºÍÔÚpython½»»¥shellÖÐÊäÈëµÄ×Ö·û´®£¬ËùÓöµ½µÄÇé¿ö»ù±¾Ò»Ñù¡£µ«³ÌÐòÖг£³£Óõ½´ÓÍøÂ磬Îļþ¶ÁÈ¡µÄÊý¾Ý£¬¹Ê´Ëµ¥¶ÀÁгö£¬ÐèÒªÌرð×¢ÒâÆä±àÂë¸ñʽÊÇ·ñÓÚϵͳҪÇóÏà·û¡£
ÏÂÃæÊÇbaiduÖÐÎı¾ÖܽðÇú°ñµÄÁ´½Ó£¬·µ»ØÒ»¸öxmlÎļþ£¬±àÂë¸ñʽΪgb2312
http://box.zhangmen.baidu.com/x?op=4&listid=1
ÓÉÓÚxml.etree.EelementTree.parse()²»Ê¶±ðgb2312±àÂ룬ÔÚ½âÎöµÄʱºò£¬ÐèÒª½«Æäת»»utf8¸ñʽ²Å¿ÉÒÔ£¬¿ÉÒÔʹÓÃÏÂÃæµÄº¯Êý
def read_page(url):
"""¶ÁÈ¡gb2312±àÂëµÄxmlÎļþ£¬×ª»»Îªutf8¸ñʽ"""
import urllib
udata = urllib.urlopen(url).read().decode('gb2312')
u8data = udata.encode('utf8')
return u8data.replace('gb2312', 'utf-8') #¼òµ¥Ìæ»»xmlÎļþµÚÒ»ÐеÄencodingÊôÐÔÖµ
ÁíÍ⣬¿ÉÒÔʹÓÃÒ»¸öСº¯ÊýÀ´ÅжÏÊý¾ÝµÄ±àÂë¸ñʽ£º
def encoding(s):
cl = ['utf8', 'gb2312']
for a in cl:
try:
s.decode(a)
return a
except UnicodeEncodeError:
pass
return 'unknown'
±¾ÎÄÀ´×ÔCSDN²©¿Í£¬×ªÔØÇë±êÃ÷³ö´¦£ºhttp://blog.csdn.net/jiyucn/archive/2008/02/16/2100006.aspx


Ïà¹ØÎĵµ£º

Óà python ¿ìËٴÍøÓηþÎñÆ÷

ÀµÓºƣ¨http://laiyonghao.com£©
½ñÌ죨2009Äê5ÔÂ31ÈÕ£© OurPNP.org ¸ãÁ˸ö¾Û»á»î¶¯£¬ÅªÁËÊ®¼¸¶þÊ®¸öÈËÔÚ¹ãÖݺ£Öé¹ã³¡µÄÌûá´ôÁËÎå¸öСʱ£¬´´ÏÂÁËÎÒÔÚ K ·¿´ôµÄ×ʱ¼ä¼Í¼¡£Ó¦ËûÃǵÄÑûÇ룬ÎÒ×öÁ˸öÌâΪ¡¶Óà python ¿ìËٴÍøÓηþÎñÆ÷¡·µÄСÑݽ²£¬ÒòΪÄDZߵĵçÊÓ¾¹È»²»ÄܽӵçÄÔ£¬ËùÒÔ½²µÄʱºòûÓÐÄܹ»²ÎÕÕ PPT À´½²£¬¹Û ......

ʹÓà Notepad++ ±à¼­ÔËÐÐ Python ³ÌÐò


Posted on 18:40:00 by ÏþÔÂ and filed under Notepad++, Python
¡¡¡¡Notepad++ ÊÇÒ»¸ö¿ªÔ´µÄÎı¾±à¼­Æ÷£¬¹¦ÄÜÇ¿´ó¶øÇÒʹÓ÷½±ã¡£±à¼­ºÍµ÷ÊÔ Python ³ÌÐòʹÓÃʲô±à¼­Æ÷»òÕß IDE ²»Í¬ÈËÓв»Í¬¼û½â¡£ÔÚ²»¿¼ÂÇʹÓõ÷ÊÔ¹¤¾ßµÄÇé¿öÏ£¬Vim »òÕß Emacs Óû§×ÔÈ»¿ÉÒÔÎÞÊÓÆäËû±à¼­Æ÷£¬²»¹ýÔÚ Windows »·¾³Ï£¬Ê¹Óà Notepad++ Ö ......

pythonѧϰ±Ê¼Ç

typeÏà¹Ø£º
ËùÓÐ×Ô¶¨ÒåÀàA
ÆäʵÀý£¬Èç a = A()
ʹÓÃtypeÔËËã·û·µ»ØµÄ¶¼ÊÇ<type, 'instance'>
¶ø»ù±¾ÀàÐÍ
±ÈÈç b = 3
type(b)£¬½á¹ûÊÇ<type, 'int'>
pythonµÄtype²»ÏñC++ÖеÄtypeidÄÇÑù£¬¿ÉÒÔÏÔʾÀàÃû¡£
£¨×¢£º¶ÔÓÚûÓÐvirtualº¯ÊýµÄÀà¶øÑÔ£¬typeidÊDZàÒëʱÆÚµÄÊÂÇ飨Ҳ¾ÍÊǾ²Ì¬ÀàÐÍ£©£»¶ÔÓÚÓÐvirtualº¯ ......

LinuxÏÂÓÃPYTHON²éÕÒͬÃû½ø³Ì

1.¿ÉÖ´ÐгÌÐò
os.system('pgrep %s > %s' % (process, output))
   pidfile = open("output", 'r')
   totalpid = len(pidfile.readlines())
   pidfile.close()
   if totalpid == 0 :
         &nbs ......

[Python module] multiprocessing


multiprocessing — Process-based “threading” interface
Introduction
multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Glo ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ