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
Ïà¹ØÎĵµ£º
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
e.g.1
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('ÓÖÌí¼ÓÁËÒ»ÐС£')
aFile.close()
output:
e.g.2
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('ÓÖÌí¼ÓÁËÒ»ÐС£')
aFile.close()
output:
e.g.3
ʵÏÖ¸ù¾ÝÔʼÎļþÓÐûÓÐ×îºóÒ»ÐпÕÐеÄÇé¿öÀ´½øÐÐ&ldqu ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 033
class Person:
age = 22
def sayHello(self): # ·½·¨Ó뺯ÊýµÄÇø±ðÔÚÓÚÐèÒªÒ»¸öself²ÎÊý
print 'Hello!'
# end of def
# end of class # ÕâÊÇÎÒÁ¼ºÃµÄ±à³Ì·ç¸ñ
p = Person()
print p.age
p.sayHello()
output£º
22
Hello! ......
˵ʵ»°£¬PythonÕæµÄ²»Ì«ÊʺÏ×öÕâÖÖ¶þ½øÖƵĶ«Î÷£¬ÌìÉúûÓÐÖ¸Õ룬µ¼ÖÂÔÚC/C++ºÜÈÝÒ׵Ķ«Î÷ÔÚPythonϾͺÜÂé·³¡£²»¹ýºÃÏñ3.1ÓÐÁËÔÉúµÄbytesÀàÐÍ£¬²»ÖªµÀÄܲ»ÄܸıäÏÖ×´¡£
import sys
import time
import socket
import struct
import random
def SendPacketData (Buffer = None , DestIP = "127.0.0.1" , DestPort = 0 ......
½ñÌì¿´ÁËÐòÁÐÀàÐÍÏà¹ØµÄ±È½Ï²Ù×÷.
ÔÚpythonºËÐıà³Ì(2nd)Ò»ÊéÖÐ6.13.1Õ½ÚÖÐ, ¸ø³öÁËÁбí±È½ÏµÄÒ»¸ö×¼Ôò..
¸öÈ˸оõ»¹ÊDz»ºÜÍêÉÆ:
Èç¹ûɨÃèµ½Á½¸öÁбíÖе±Ç°±È½ÏÔªËØÊDz»¿É±È½ÏµÄ, ÄÇô·µ»Øʲô??
ÎÒÓõÄÊÇpython2.6....
¶ÔÕâ¸öÎÊÌâ×öÁËһЩ²âÊÔ, ×Ô¼ºÄ¿Ç°¸Â¾øµ±±È½ÏÓöµ½ÉÏÊöÇé¿öʱ, ÊÇʹÓÃÁ½¸öÁбíµÄÄÚ´æµØÖ·ÖµÀ´±È½ ......
filename=raw_input('enter file name:')
f=open(filename,'rb')
f.seek(0,0)
index=0
for i in range(0,16):
print "%3s" % hex(i) ,
print
for i in range(0,16):
print "%-3s" % "#" ,
print
while True:
temp=f.read(1)
if len(temp) == 0:
break
else:
print "%3s" % temp.encode('hex'),
......