python±Ê¼ÇÖ®ÕýÔò±í´ïʽ
ÕýÔò±í´ïʽ
¾ßÌåµÄ²Î¿¼ÊֲᣬÕâÀï¼ÇÏÂһЩСÎÊÌ⣺
1¡¢re¶ÔÏóµÄ·½·¨
match Match a regular expression pattern to the beginning of a string.
search re.search(pattern, string, flags) flags:re.I re.M re.X re.S re.L re.U
sub Substitute occurrences of a pattern found in a string.
subn Same as sub, but also return the number of substitutions made.
split Split a string by the occurrences of a pattern.
findall Find all occurrences of a pattern in a string.
finditer Return an iterator yielding a match object for each match.
iter = re.finditer("23", "123423523")
for i in iter:
print i.span()
compile ½«Ò»¸öpattern±àÒë³ÉÒ»¸öRegexObject.
re¶ÔÏóµÄʹÓ÷½·¨£¬reobject = re.complie(pattern)£¬´Óreobject»ñµÃpatternµÄ·½·¨£¬Ê¹ÓÃreobject.patternÊôÐÔ
Èç¹ûÒ»¸öreobjectÒª¶à´ÎʹÓã¬×îºÃʹÓÃcompile·½·¨Ìá¸ßÐÔÄÜ£¬·ñÔò¿ÉÒÔÖ±½Ó°´ÕâÖÖ·½Ê½Ê¹Óã¬re.search(pattern, string)
purge Clear the regular expression cache.
escape Backslash all non-alphanumerics in a string.
2¡¢ÒªÊ¹group(s)·½·¨£¬Ó¦¸ÃÔÚpatternÖжÔÏëÒªÔÚtuplesÖеIJ¿·Ö¼ÓÉÏÀ¨ºÅ£¬ÀýÈç“123-45”£¬ÏëÆ¥ÅäÕâ¸östring£¬µ«ÊÇÖ»Ïë»ñµÃǰÈý¸öÊý×Ö£¬Ôò¿ÉÒÔʹÓÃÕâÑùµÄpattern,"(\d{3})-\d{2}"
,×¢ÒâÇ°ÃæÊ¹ÓÃÁËÀ¨ºÅ£¬ÔÙµ÷ÓÃgroups·½·¨»áµÃµ½tuples,("123",)
¡£group(s)ÊÇSRE_Match object µÄ·½·¨£¬Í¨³£Ê¹Ó÷½·¨Îªreobject.search(string).group(s)
3¡¢ÒªÆ¥ÅäÈÎÒâµ¥¸ö×Ö·ûʹÓÃ"."£¬ÈÎÒâ¸ö×Ö·ûʹÓÃ".*"(°üÀ¨0¸ö)£¬»òÕß".+"(ÖÁÉÙÒ»¸ö)¡£
4¡¢À¨ºÅµÄ¶àÖÖÓ÷¨
(?:)²»·ÅÈëgroupsÖÐ
(?iLmsux) Ϊpattern¼ÓÈëI,L,M,S,U,X±êÖ¾
(?P<name>) ΪgroupµÄÒ»Ïî¼ÓÈë±ðÃû
(?P=name) Æ¥ÅäÔÚ֮ǰÓÃname±íʾµÄ²¿·Ö±í´ïʽËùÆ¥Åäµ½µÄÄÚÈÝ
(?#comment) ×¢ÊÍ
test1(?=test2) Èç¹ûtest1ºóÃæ¸ú×Åte
Ïà¹ØÎĵµ£º
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
# 032
# ÆäʵcPickleÕâ¸öÄ£¿éÆðµ½µÄ×÷ÓÿÉÒÔÓÓÍêÃÀµØÐµ÷ÁËÎļþÖеÄÄÚÈÝ£¨¶ÔÏ󣩺ʹúÂëÖеÄÒýÓÔÀ´ÐÎÈÝ
import cPickle as p # ÕâÌõÓï¾ä¸øcPickleÆðÁ˸öСÃûp
objectFileName = r'C:\Data.txt'
aList = [1, 2, 3]
f = file(objectFileName, 'w')
p.dump(aList, f)
f.close ......
# 040
import time
try:
f = file('040_Finally.py')
while True:
line = f.readline()
if len(line) == 0:
break
time.sleep(0.33)
print line,
# end of while
finally:
f.close()
print 'Closed the file.'
# end of try
output£º
> ......
µÚ¾Å¹Ø Image
´ÓÒ³ÃæÉϵÄͼƬ¿ÉÒÔ¿´µ½ÓÐÒ»´®µã£¬ÄÇôÊDz»ÊÇ´ú±í¸Ã¹ØÓëͼÏñµãÓйأ¿ ÎÒÃÇ´ÓÒ³ÃæÔ´Âë¿ÉÒÔ¿´µ½£¬ÓÐÁ½¶ÎÊý×ÖÐòÁÐfirstºÍsecond£¬¶øÓÐÒ»¸öÌáʾfirst+second=? ʲôÒâË¼ÄØ£¿ÄѵÀÊÇ˵(first, second)´ú±íÁËͼÏñµãµÄ×ø±ê£¿²»Ïñ£¬Á½¶ÎÐòÁеij¤¶ÈÓкܴó²îÒì¡£ÄÇôËã·û+»¹ÓÐʲôº¬ÒåÄØ£¬Óп ......
¿ÉÒÔ²¥·Å´ó²¿·ÖµÄÒôÊÓÆµ.
demo download: http://www.sandy1219.com/python/media.rar
playMP3.py
# -*- coding: utf-8 -*-
import wx;
import wx.media;
import os;
import SPrint;
import mediaStateBar;
import mediaList;
import SaveLog;
import MediaItem;
woldcart = "media files|*.*|avi ......
Ê×ÏÈÊÇÏÂÔØpython3£¬ÏÖÔÚµÄ×î¸ß°æ±¾ÊÇ3.1.1
for linux¡£
ÎҵķÅÖ÷¾¶ÊÇ/home/pythonÏ·ÅÖÃPython-3.1.1.tgz,Ö´ÐÐÒÔÏÂϵÁвÙ×÷£º
1.½âѹ£ºtar zxvf Python-3.1.1.tgz----Éú³É½âѹ°üPython-3.1.1
2.ת»»µ½Python-3.1.1·¾¶Ï£¬Ö´ÐÐ./configure
3.make
4.make install
ÔÚrehl5ÖÐÒѾĬÈϰ²×°ÁËpython2.4,ËùÒÔÒª×öÈçÏ ......