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µÄ´æÔÚ£¬´Ó×î³õµÄ²»¾Ò⣬µ½Ô½À´Ô½Ï²°®£¬ÎÒÒ»²½²½µÄÏòpython¿¿½ü¡£
ÎÒϲ»¶Python£¬ÕâÊÇÒ»¸öÊÂʵ£¬ÎÒ׫´ ......
½ñÌìÓöµ½ÁËÂé·³£º
ÓÃEclipse±àÒëpythonÏòsqliteÊý¾Ý¿â²åÈëÊý¾Ý×ÜÊÇÌáʾ±àÂëµÄÎÊÌ⣬ÎÒÉèÖÃÁËEclipseµÄworkspaceÓÃutf-8±àÂ뻹ÊDz»ÐÐ
µ«ÊÇÓÃEclipseдµÄ³ÌÐò±£´æÔÙÓÃIDLE´ò¿ªÈ´ÄܱàÒ룬Ææ¹ÖÁË
ÎÒ¿¼Âǵ½¿ÉÄÜÊÇÅäÖõÄÔÒò£¬ÕÒÕÒ±éÁËËùÓеÄÅäÖ㬶¼Ã»ÓÐÎÊÌ⣬°üÀ¨¹¤³ÌÎļþ
×îºóÎÒÓÃxvi32´ò¿ ......
ǰһƪÎÄÕÂдµÄÔÚAPACHE°²×°MOD_PYTHONµÄ¾¹ý,Æäʵͦ¼òµ¥,¾ÍÊÇ°æ±¾²»¼æÈݵÄÎÊÌâ.Õâ´ÎÎÒ´ó¸Å˵ϲ¿ÊðDJANGOµÄ¹ý³Ì.
ÏÈÐÞ¸ÄAPACHEÅäÖÃÎļþ,ʹÆä¼ÓÔØmod_pythonÄ£¿é
LoadModule python_module libexec/mod_python.so
ÔËÐÐÃüÁî²é¿´
bin/httpd -M¿ÉÒÔ¿´µ½
python_module (shared)
Syntax OK
˵Ã÷apacheÒѾ³É¹¦¼Ó ......
½ñÌìÊǵڶþÌì×Ô¼º¿´¹ØÓÚPythonÁË£¬¿´¼ûÒ»¸öPython2дµÄ°Ù¶È´Êµä£¬ÎÒÒ²ÓÃPython 3 дÁËÒ»¸ö¡£ÕæµÄºÜСÇÉ£¬ºÇºÇ£¬ºÜºÃµÄÓïÑÔ¡£
²»ÖªµÀÔõôÉÏ´«´úÂë¸ñʽµÄ£¬¾ÍÉÏ´«Îı¾ÁË£º
# -*- coding: utf8 -*-
import urllib.parse
import urllib.request
def search(word):
#word = input("ÊäÈëÄãÒª²éѯµÄ ......