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)
½á¹û£º 
Ïà¹ØÎĵµ£º
#from pp3e Chapter 9.3
#############################################################################
# popup three new window, with style
# destroy() kills one window, quit() kills all windows and app; top-level
# windows have title, icon, iconify/deiconify and protocol for wm events;
# there ......
1.open
ʹÓÃopen´ò¿ªÎļþºóÒ»¶¨Òª¼ÇµÃµ÷ÓÃÎļþ¶ÔÏóµÄclose()·½·¨¡£±ÈÈç¿ÉÒÔÓÃtry/finallyÓï¾äÀ´È·±£×îºóÄܹرÕÎļþ¡£
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
×¢£º²»Ä ......
ÓÃPythonÌáÈ¡Îļþ¼ÐϵÄÌض¨À©Õ¹ÃûµÄÎļþ
²»ÖªµÀʲôʱºò£¬ÍøÏÐ×ÅûÓ㬹ÒÁ˸ölinuxµÄÊÓÆÁ½Ì³Ì£¬ÀïÃæÓкܶà¸öÎļþ¼Ð£¬ÓкܶàÎ޹صÄÎļþ¡£Õâ¶ÔÓÚÏñÎÒûÊÕ²ØÀ¬»øÎļþñ±ºÃµÄÈËÀ´Ëµ£¬¼òÖ±ÊÇ ......
refer from: http://www.daniweb.com/forums/thread115282.html#
python
Syntax
(Toggle Plain Text
)
# respond to a key
without the need to press
enter
import
Tkinter
as tk
def
keypress(
event)
:
if
event.keysym
== 'Escape'
:
root.destroy
......
ǰһƪÎÄÕÂдµÄÔÚAPACHE°²×°MOD_PYTHONµÄ¾¹ý,Æäʵͦ¼òµ¥,¾ÍÊÇ°æ±¾²»¼æÈݵÄÎÊÌâ.Õâ´ÎÎÒ´ó¸Å˵ϲ¿ÊðDJANGOµÄ¹ý³Ì.
ÏÈÐÞ¸ÄAPACHEÅäÖÃÎļþ,ʹÆä¼ÓÔØmod_pythonÄ£¿é
LoadModule python_module libexec/mod_python.so
ÔËÐÐÃüÁî²é¿´
bin/httpd -M¿ÉÒÔ¿´µ½
python_module (shared)
Syntax OK
˵Ã÷apacheÒѾ³É¹¦¼Ó ......