具体的实例我们需要在目录中遍历,包括子目录(哈哈),找出所有后缀为:rmvb,avi,pmp的文件。(天哪?!你要干什么?这可是我的隐私啊~~)[code]importosdefanyTrue(predicate,sequence):returnTrueinmap(predicate,sequence)deffilterFiles(folder,exts):forfileNameinos.listdir(folder):ifos.path.isdir(folder+'/'+fileName):filterFiles(folder+'/'+fileName,exts)elifanyTrue(fi...
http://www.jb51.net//article/15708.htm
就其本质而言,正则表达式(或RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过re模块实现。使用这个小型语言,你可以为想要匹配的相应字符串集指定规则;该字符串集可能包含英文语句、e-mail地址、TeX命令或任何你想搞定的东西。然後你可以问诸如“这个字符串匹配该模式吗?”或“在这个字符串中是否有部分匹配该模式呢?”。你也可以使用RE以各种方式来修改或分割字符串。正则表达式模式被编译成一系列的字节码,然後由用C编写的匹配引擎执行。在高级用法中,也许还要仔细留意引擎是如何执行给定RE,如何以特定方式编写RE以令生产的字节码运行速度更快。本文并不涉及优化,因...
http://www.jb51.net//article/15707.htm
[code]mulLine="""Hello!!!WellcometoPython'sworld!Therearealotofinterestingthings!Enjoyyourself.Thankyou!"""print''.join(mulLine.splitlines())print'------------'print''.join(mulLine.splitlines(True))[/code]输出结果:Hello!!!WellcometoPython'sworld!Therearealotofinterestingthings!Enjoyyourself.Thankyou!---...
http://www.jb51.net//article/15706.htm
1.设置fomat格式,如下:[code]#取前5个字符,跳过4个字符华,再取3个字符format='5s4x3s'[/code]2.使用struck.unpack获取子字符串[code]importstructprintstruct.unpack(format,'Testastring')#('Test','ing')[/code]来个简单的例子吧,有一个字符串'Heisnotveryhappy',处理一下,把中间的not去掉,然后再输出。[code]importstructtheString='Heisnotveryhappy'format='2s1x2s5x4s1x5s'print''....
http://www.jb51.net//article/15705.htm
return(1==1)?"iseasy":"mygod"//C#中的用法其实,在Python中,是这样写的:print(1==2)and'Fool'or'Notbad'输出结果:Notbad...
http://www.jb51.net//article/15704.htm
函数较简单,看下面的例子:[code]s='hEllopYthon'prints.upper()prints.lower()prints.capitalize()prints.title()[/code]输出结果:HELLOPYTHONhellopythonHellopythonHelloPython判断大小写Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写。注意的是:1.没有提供iscapitalize()方法,下面我们会自己实现,至于为什么Python没有为我们实现,就不得而知了。2.如果对空字符串使用isupper(),islower...
http://www.jb51.net//article/15703.htm
[code]from__future__importdivisionprint7/3[/code]输出结果:2.3333333333...
http://www.jb51.net//article/15702.htm
1.string.maketrans设置字符串转换规则表(translationtable)[code]allchars=string.maketrans('','')#所有的字符串,即不替换字符串aTob=string.maketrans('a','b')#将字符a转换为字符b[/code]2.translate函数进行字符串的替换和删除,第一个参数是字符串转换规则表(translationtable),第二个参数是要删除的字符串。比如,要将字符串s中的所有e替换为a,同时要删除所有的o[code]aTob=string.maketrans('e','a')s='hellopython'pr...
http://www.jb51.net//article/15701.htm
方法一,使用[::-1]:s='python'prints[::-1]方法二,使用reverse()方法:l=list(s)l.reverse()print''.join(l)输出结果:nohtypnohtyp...
http://www.jb51.net//article/15700.htm
join方法用于连接字符串数组[code]s=['a','b','c','d']print''.join(s)print'-'.join(s)[/code]输出结果:abcda-b-c-d使用%连接多个变量[code]a='hello'b='python'c=1print'%s%s%s%s'%(a,b,c,s)[/code]输出结果:hellopython1['a','b','c','d']...
http://www.jb51.net//article/15699.htm
注意的是,传入的是一个字符数组,编译器去除两端所有相应的字符,直到没有匹配的字符,比如:[code]theString='saaaayyesnoyaaaass'printtheString.strip('say')[/code]theString依次被去除首尾在['s','a','y']数组内的字符,直到字符在不数组内。所以,输出的结果为:yesno比较简单吧,lstrip和rstrip原理是一样的。注意:当没有传入参数时,是默认去除首尾空格的。[code]theString='saaaayyesnoyaaaass'printtheString.strip('say')printtheStri...
http://www.jb51.net//article/15698.htm
