Python¡¢UnicodeºÍÖÐÎÄ[ת]
¹Ø¼ü×Ö: python
Python¡¢UnicodeºÍÖÐÎÄ[ת]
pythonµÄÖÐÎÄÎÊÌâÒ»Ö±ÊÇÀ§ÈÅÐÂÊÖµÄÍ·ÌÛÎÊÌ⣬ÕâƪÎÄÕ½«¸øÄãÏêϸµØ½²½âÒ»ÏÂÕâ·½ÃæµÄ֪ʶ¡£µ±È»£¬¼¸ºõ¿ÉÒÔÈ·¶¨µÄÊÇ£¬ÔÚ½«À´µÄ°æ±¾ÖУ¬python»á³¹µ×½â¾ö´ËÎÊÌ⣬²»ÓÃÎÒÃÇÕâôÂé·³ÁË¡£
ÏÈÀ´¿´¿´pythonµÄ°æ±¾£º
>>> import sys
>>> sys.version
'2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]'
£¨Ò»£©
ÓüÇʱ¾´´½¨Ò»¸öÎļþChineseTest.py£¬Ä¬ÈÏANSI£º
s = "ÖÐÎÄ"
print s
²âÊÔÒ»ÏÂÇÆÇÆ£º
E:\Project\Python\Test>python ChineseTest.py
File "ChineseTest.py", line 1
SyntaxError: Non-ASCII character '\xd6' in file ChineseTest.py on line 1, but no encoding declared; see http://www.pytho
n.org/peps/pep-0263.html for details
͵͵µØ°ÑÎļþ±àÂë¸Ä³ÉUTF-8£º
E:\Project\Python\Test>python ChineseTest.py
File "ChineseTest.py", line 1
SyntaxError: Non-ASCII character '\xe4' in file ChineseTest.py on line 1, but no encoding declared; see http://www.pytho
n.org/peps/pep-0263.html for details
ÎÞ¼ÃÓÚÊ¡£¡£¡£
¼ÈÈ»ËüÌṩÁËÍøÖ·£¬ÄǾͿ´¿´°É¡£¼òµ¥µØä¯ÀÀһϣ¬ÖÕÓÚÖªµÀÈç¹ûÎļþÀïÓзÇASCII×Ö·û£¬ÐèÒªÔÚµÚÒ»ÐлòµÚ¶þÐÐÖ¸¶¨±àÂëÉùÃ÷¡£°ÑChineseTest.pyÎļþµÄ±àÂëÖØиÄΪANSI£¬²¢¼ÓÉϱàÂëÉùÃ÷£º
# coding=gbk
s = "ÖÐÎÄ"
print s
ÔÙÊÔһϣº
E:\Project\Python\Test>python ChineseTest.py
ÖÐÎÄ
Õý³£¿©£º£©
£¨¶þ£©
¿´Ò»¿´ËüµÄ³¤¶È£º
# coding=gbk
s = "ÖÐÎÄ"
print len(s)
½á¹û£º4¡£
sÕâÀïÊÇstrÀàÐÍ£¬ËùÒÔ¼ÆËãµÄʱºòÒ»¸öÖÐÎÄÏ൱ÓÚÁ½¸öÓ¢ÎÄ×Ö·û£¬Òò´Ë³¤¶ÈΪ4¡£
ÎÒÃÇÕâÑùд:
# coding=gbk
s = "ÖÐÎÄ"
s1 = u"ÖÐÎÄ"
s2 = unicode(s, "gbk") #Ê¡ÂÔ²ÎÊý½«ÓÃpythonĬÈϵÄASCIIÀ´½âÂë
s3 = s.decode("gbk") #°Ñstrת»»³ÉunicodeÊÇdecode£¬unicodeº¯Êý×÷ÓÃÓëÖ®Ïàͬ
print len(s1)
print len(s2)
print len(s3)
½á¹û£º 
Ïà¹ØÎĵµ£º
pythonµÄ±ä²Î
*argsºÍ**dargsÊÇPythonµÄÁ½¸ö¿É
±ä²ÎÊý£¬Á½ÕßÓÐËù²»Í¬µÄÊÇ*argsÊǸötuple£¬**dargsÊǸödict¡£
*args
ºÍ**dargs²¢ÓÃʱ£¬*args±ØÐë·ÅÔÚ**dargsµÄÇ°Ãæ¡£
ÀýÈ磺
def func(a,b, *c):
pass
º¯ÊýfuncÖÁÉÙÓÐÁ½¸ö²ÎÊý±ä²ÎÊý·ÅÔÚtuple cÖÐ
def func(*c): »òÕß def func(**d ......
1. ÊÖ¶¯ÖÆ×÷pythonµÄexe¿ÉÖ´ÐгÌÐò
תÔØ---------------
PythonûÓÐÄÚ½¨Ò»¸ö±à ......
ǰһƪÎÄÕÂдµÄÔÚAPACHE°²×°MOD_PYTHONµÄ¾¹ý,Æäʵͦ¼òµ¥,¾ÍÊÇ°æ±¾²»¼æÈݵÄÎÊÌâ.Õâ´ÎÎÒ´ó¸Å˵ϲ¿ÊðDJANGOµÄ¹ý³Ì.
ÏÈÐÞ¸ÄAPACHEÅäÖÃÎļþ,ʹÆä¼ÓÔØmod_pythonÄ£¿é
LoadModule python_module libexec/mod_python.so
ÔËÐÐÃüÁî²é¿´
bin/httpd -M¿ÉÒÔ¿´µ½
python_module (shared)
Syntax OK
˵Ã÷apacheÒѾ³É¹¦¼Ó ......
С°×¶ÔÉϽڿÎÀïµÄ“СÂéȸ”ÐËȤ°»È»£¬ÕûÌì¾ÍÔÚËÞÉáµ·¹Ä¡£Õâ²»£¬¼¸ÌìÏÂÀ´ËûÒѾ¹¥ÏÂÊ®¼¸¸öÍøÕ¾ÁË¡£
С²Ëȴϲ»¶Ò»¸öÈË×øÔÚµçÄÔÅԱߣ¬Öå×Åüͷ£¬ÐÀÉÍ×ÅÕâÒ»ÐÐÐÐ×Ô¼º¿´²»¶®µÄ´úÂë¡£“ÕâЩ×Ö·ûºÃ¹ÖÒìѽ£¡”С²ËÍá×ÅÍ·£¬“ËüΪɶ½ÐÕýÔò±í´ïʽÄØ£¿”
°Ù¶Èһϣ¬ÖÕÓÚÖªµÀ¡£Ð¡² ......
µ±Ö´ÐÐimport
moduleʱ£¬½âÊÍÆ÷»á¸ù¾ÝÏÂÃæµÄËÑË÷·¾¶£¬ËÑË÷module1.pyÎļþ¡£
1) µ±Ç°¹¤×÷Ŀ¼
2) PYTHONPATHÖеÄĿ¼
3) Python°²×°Ä¿Â¼
(/usr/local/lib/python)
ÊÂʵÉÏ£¬Ä£¿éËÑË÷ÊÇÔÚ±£´æÔÚsys.pathÕâ¸öÈ«¾Ö±äÁ¿ÖеÄĿ¼ÁбíÖнøÐÐËÑË÷¡£
sys.path»áÔÚ½âÊÍÆ÷¿ªÊ¼Ö´ÐÐʱ±»³õʼ»¯³É°üº¬£º
1)µ±Ç°¹¤×÷Ŀ¼
2) PYT ......