zzʹÓà Python ·ÖÀëÖÐÎÄÓëÓ¢ÎĵĻìºÏ×Ö´®
ʹÓà Python ·ÖÀëÖÐÎÄÓëÓ¢ÎĵĻìºÏ×Ö´®
LiYanrui
posted @ ´óÔ¼ 1 Äêǰ
in ³ÌÐòÉè¼Æ
with tags
python
, 614 ÔĶÁ
Õâ¸öÎÊÌâÊÇ×ö MkIV Ô¤´¦Àí³ÌÐò
ʱ¸ã¶¨µÄ£¬¾ÍÊǰÑÒ»¸ö»ìºÏÁËÖÐÓ¢ÎÄ»ìºÏ×Ö´®·ÖÀëΪӢÎÄÓëÖÐÎĵÄ×Ó×Ö´®£¬Æ©È磬½« ”ÎÒµÄ English ѧµÄ²»ºÃ
“ ·ÖÀëΪ “ÎÒµÄ"
¡¢" English ”
Óë "ѧµÄ²»ºÃ"
Èý¸ö×Ó×Ö´®¡£
1. ÖÐÓ¢ÎÄ»ìºÏ×Ö´®µÄͳһ±àÂë±íʾ
ÖÐÓ¢ÎÄ»ìºÏ×Ö´®´¦Àí×îÊ¡Á¦µÄ°ì·¨¾ÍÊǰÑËüÃǵıàÂ붼ת³É Unicode£¬ÈÃÒ»¸öºº×ÖÓëÒ»¸öÓ¢ÎÄ×ÖĸµÄÄÚ´æÎ»¿í¶¼ÊÇÏàµÈµÄ¡£Õâ¸ö¹¤×÷ÓÃ
Python À´×ö£¬±È½ÏºÏÊÊ£¬ÒòΪ Python ÄÚÂë²ÉÓõÄÊÇ Unicode£¬²¢ÇÒΪÁËÖ§³Ö Unicode ×Ö´®µÄ²Ù×÷£¬Python
×öÁËÒ»¸ö Unicode ÄÚ½¨Ä£¿é£¬°Ñ string ¶ÔÏóµÄÈ«²¿·½·¨ÖØÐÂʵÏÖÁËÒ»±é£¬ÁíÍâÌṩÁË Codecs
¶ÔÏ󣬽â¾ö¸÷ÖÖ±àÂëÀàÐ͵Ä×Ö·û´®½âÂëÓë±àÂëÎÊÌâ¡£
Æ©ÈçÏÂÃæµÄ Python ´úÂ룬¿ÉʵÏÖ UTF-8 ±àÂëµÄÖÐÓ¢ÎÄ»ìºÏ×Ö´®Ïò Unicode ±àÂëµÄת»»£º
# -*- coding:utf-8 -*-
a = "ÎÒµÄ English ѧµÄ²»ºÃ"
print
type
(
a)
,len
(
a)
, a
b = unicode
(
a, "utf-8"
)
print
type
(
b)
, len
(
b)
, b
×Ö·û´® a ÊÇ utf-8 ±àÂ룬ʹÓà python µÄÄÚ½¨¶ÔÏó unicode ¿É½«Æäת»»Îª Unicode ±àÂëµÄ×Ö·û´® b¡£ÉÏÊö´úÂëÖ´ÐкóµÄÊä³ö½á¹ûÈçÏÂËùʾ£¬±È½Ï×Ö´® a Óë×Ö´® b µÄ³¤¶È£¬ÏÔÈ» len (b) µÄÊä³ö½á¹ûÊǺÏÀíµÄ¡£
<type 'str'
> 27
ÎÒµÄ English ѧµÄ²»ºÃ
<type 'unicode'
> 15
ÎÒµÄ English ѧµÄ²»ºÃ
ҪעÒâµÄÒ»¸öÎÊÌâÊÇ Unicode ËäÈ»ºÅ³ÆÊǓͳһÂ딣¬²»¹ýÒ²ÊÇ´æÔÚ×ÅÁ½ÖÖÐÎʽ£¬¼´£º
UCS-2£ºÎª 16 λÂ룬¾ßÓÐ 2^16 = 65536 ¸öÂëλ£»
UCS-4£ºÎª 32 λÂ룬ĿǰµÄ¹æ¶¨ÊÇÆäÊ××Ö½ÚµÄÊ×λΪ 0£¬Òò´Ë¾ßÓÐ 2^31 = 2147483648 ¸öÂë룬²»¹ýÏÖÔÚµÄֻʹÓÃÁË 0x00000000 £ 0x0010FFFF Ö®¼äµÄÂë룬¹² 1114112 ¸ö¡£
ʹÓÃPython sys Ä£¿éÌṩµÄÒ»¸ö±äÁ¿ maxunicode µÄÖµ¿ÉÒÔÅжϵ±Ç° Python ËùʹÓÃµÄ Unicode ÀàÐÍÊÇ UCS-2 µÄ»¹ÊÇ UCS-4 µÄ¡£
import
sys
print
sys
.maxunicode
Èô sys.maxunicode µÄֵΪ 1114111£¬¼´Îª UCS-4£»ÈôΪ 65535£¬ÔòΪ UCS-2¡£
2. ÖÐÓ¢ÎÄ»ìºÏ×Ö´®µÄ·ÖÀë
Ò»µ©ÖÐÓ¢ÎÄ×Ö´®µÄ±àÂë»ñµÃͳһ£¬ÄÇô¶ÔËüÃǽøÐзÖÁѾÍÊǺܼòµ¥µÄÊÂÇéÁË¡£Ê×ÏÈҪΪÖÐÎÄ×Ö´®ÓëÓ¢ÎÄ×Ö´®·Ö±ð×¼±¸
Ïà¹ØÎĵµ£º
ÔÚÉÏһƪÖÐÎÒÃÇÒѾʹÓÃcÓïÑÔʵÏÖÁËÒ»¸ö×î¼òµ¥µÄÀ©Õ¹Ä£¿é£¬ÕâһƪÖн«ÔÚÆä»ù´¡ÉϽøÐй¦Äܵķḻ¡£Ê×ÏÈÀ´¿¼ÂÇÈçºÎ´ÓÍⲿµÄPythonÏòCÄ£¿é´«µÝ½ø²ÎÊý£¬foo_bar2չʾÁËÈçºÎÏòCÄ£¿é´«µÝÕûÊý£¬¸¡µãÊý£¬×Ö·û´®Èý¸ö²ÎÊý£¬ÆäÖÐ"ids"Ö¸Ã÷ÁË´«Èë²ÎÊýµÄÊý¾ÝÀàÐÍ¡£PyArg_ParseTuple¸ºÔð¶Ôargs½øÐнâÎö£¬Èô½âÎöʧ°ÜÔò·µ»Ø0.´úÂë#include&n ......
Python¼òµ¥ÓÖ¹¦ÄÜÇ¿´ó¡£Ëü×¢ÖØµÄÊÇÈçºÎ½â¾öÎÊÌ⣬¶ø²»ÊDZà³ÌÓïÑÔµÄÓï·¨ºÍ½á¹¹¡£ÀíÏëµÄ½Å±¾ÓïÑÔ¡£ÊÊÓÃÓÚ¿ìËÙµÄÓ¦ÓóÌÐò¿ª·¢¡£
PythonÊÇÒ»ÖÖ½âÊÍÐÔÓïÑÔ¡£»¹ÊÇÒ»ÖÖÃæÏò¶ÔÏóµÄÓïÑÔ¡£
ÓÐÁ½ÖÖʹÓÃpythonÔËÐÐÄãµÄ³ÌÐòµÄ·½Ê½£º
ʹÓý»»¥Ê½µÄ´øÌáʾ·ûµÄ½âÊÍÆ÷
ʹ ......
eval(str [,globals [,locals ]])º¯Êý½«×Ö·û´®strµ±³ÉÓÐЧPython±í´ïʽÀ´ÇóÖµ£¬²¢·µ»Ø¼ÆËã½á¹û¡£
ͬÑùµØ, execÓï¾ä½«×Ö·û´®strµ±³ÉÓÐЧPython´úÂëÀ´Ö´ÐÐ.Ìṩ¸øexecµÄ´úÂëµÄÃû³Æ¿Õ¼äºÍexecÓï¾äµÄÃû³Æ¿Õ¼äÏàͬ.
×îºó£¬execfile(filename [,globals [,locals ]])º¯Êý¿ÉÒÔÓÃÀ´Ö´ÐÐÒ»¸öÎļþ,¿´ÏÂÃæµÄÀý×Ó:
>>> ev ......
PythonÌ«»ðÁË£¬²»Ñ§µã¶¼¸Ð¾õ×Ô¼º²»ÊÇѧ¼ÆËã»úµÄ£¬½ñÌì¿´Á˸ö²»´íµÄ¡¶¼òÃ÷python½Ì³Ì¡·£¬ºÜ²»´í¡£²»¹ýÔÚѧϰ¹ý³ÌÖУ¬¾ÓÈ»·¢ÏÖÁËÒ»¸öPythonµÄbug£¬
#!/usr/bin/python
#coding=UTF-8
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person's data.'''
......
±¾ÎÄ×ܽáÁËÔÚPythonÖÐÖ÷ÒªµÄ¼¸ÖÖ´¦ÀíXMLµÄ·½·¨£º
Element Tree
SAX
Dom
ʹÓõÚÈý·½Àà¿âÈçAmara 2.x£¬ libxml2dom µÈ
Element Tree
Element TreeÊÇPython 2.5ÒýÈëµÄʹÓüòµ¥£¬¿ì½ÝµÄ´¦Àí·½·¨£¬ Ò²ÊÇPython±ê×¼Àà¿âÍÆ¼öµÄ´¦ÀíXMLµÄ·½·¨¡£
ËüʹÓÃÊ÷ÐνڵãµÄÐÎʽÀ´¶ÁдXML¡£Element TreeÊÇÇáÁ¿¼¶µÄDOM£¬ ËùÒÔÊ ......