`
hugh.wangp
  • 浏览: 289746 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

PYTHON-字符串处理函数

阅读更多

在互联网行业,文本数据远大于结构化的数据,海量的数据的文本处理也是迫在眉睫。

 

 

字符串的字符操作
>>> s='hello World! Everyone! This Is My First String!'
>>> s   #打印出s的内容
'hello World! Everyone! This Is My First String!'
>>> s.lower()  #小写字符串s
'hello world! everyone! this is my first string!'
>>> s.upper()  #大写字符串s
'HELLO WORLD! EVERYONE! THIS IS MY FIRST STRING!'
>>> s.swapcase() #大小写互换
'HELLO wORLD! eVERYONE! tHIS iS mY fIRST sTRING!'
>>> s.capitalize() #整个字符串的首字符大写
'Hello world! everyone! this is my first string!'
>>> s.title()  #字符串每个单词的首字符大写(以空格、tab制表符、换行符隔开的单词)
'Hello World! Everyone! This Is My First String!'

 

字符串的格式操作
>>> s
'a'
>>> s.ljust(10,'*') #s.ljust(width,[fillchar])左对齐输出10个字符,不足部分用'*'补足,默认用空格补足
'a*********'
>>> s.rjust(10,'*') #s.rjust(width,[fillchar])右对齐输出10个字符,不足部分用'*'补足,默认用空格补足
'*********a'
>>> s.center(10,'*') #s.center(width,[fillchar])中间对齐,把s放在10个字符中间,不足部分两边用'*'补足
'****a*****'
>>> s.zfill(10) #s.zfill(width)右对齐输出10个字符,不足部分只能用'0'补足,功能同s.rjust(10,'0')
'000000000a'

 

字符串中的搜索和替换
 >>> s
 'hello World! Everyone! This Is My First String!'
 >>> s.find('e',4,20)
 15
 >>> s.find('e',4)
 15
 >>> s.find('e')
 1
 >>> s.find('e',4,3)
 -1
 >>> s.find('z')
 -1
#s.find(substr, [start, [end]])
#从字符串s的[start:end]的子串中查找substr,第一次匹配到substr并返回substr第一个字符的位置
#这里的substr也是个字符串,并且是大小写敏感。默认从字符串首到串尾的范围内查找,s中没有substr的话返回-1
#如果end<start,返回-1


 >>> s.index('e',4,20)
 15
 >>> s.index('e',4)
 15
 >>> s.index('e')
 1
 >>> s.index('e',4,3)
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 ValueError: substring not found
#s.index(substr, [start, [end]])
#功能与find()相同,唯一不同的是s中没有substr时不是返回-1而是返回一个运行错误


 >>> s.rfind('e',4,20)
 15
 >>> s.rfind('e',4)
 20
 >>> s.rfind('e')
 20
#s.rfind(substr, [start, [end]])
#和find()的区别是最后一次匹配到substr并返回substr第一个字符的位置
#s.rindex(substr, [start, [end]])
#和index()的区别之于rfind()和find()的区别

 

 >>> s.count('e',4,20)
 1
 >>> s.count('e',4)
 2
 >>> s.count('e')
 3
 >>> s.count('z')
 0
#s.count(substr, [start, [end]])
#计算substr在s的[start:end]的范围内出现的次数。没有出现返回0

 

 >>> s.replace('r','R')
 'hello WoRld! EveRyone! This Is My FiRst StRing!'
 >>> s.replace('r','R',1)
 'hello WoRld! Everyone! This Is My First String!'
 >>> s.replace('z','Z')
 'hello World! Everyone! This Is My First String!'
#s.replace(oldstr, newstr, [count])
#把oldstr替换为newstr, count为替换次数,没有设置count这个参数,那就是全部替换
#如果oldstr没有在s中出现,返回s的原值


 >>> s               
 '   hello/t/n'      
 >>> s.expandtabs(2) 
 '   hello  /n'      
 >>>                 
 >>> s='   hello/t/n'
 >>> s               
 '   hello/t/n'      
 >>> s.strip()       
 'hello'             
 >>> s.lstrip()      
 'hello/t/n'         
 >>> s.rstrip()      
 '   hello'    
 >>> s.expandtabs()  
 '   hello        /n'
 >>> s.expandtabs(0)
 '   hello/n' 
 >>> s='hello/t/n' 
 >>> s
 'hello/t/n'
 >>> s.strip('h')
 'ello/t/n'   
#s.strip([chars]) 
#s.lstrip([chars]) 
#s.rstrip([chars]) 
#s.expandtabs([tabsize])
#strip()是把s前后的chars全部去掉,相当于把chars替换成None。参数默认为空格、Tab制表符/t、换行符/n
#lstrip()和strip()的区别是它只处理s串头的chars
#rstrip()和strip()的区别是它只处理s串尾的chars
#expandtabs()把tab制表符替换成tabsize个空格

 

字符串的分割和组合
 >>> s
 'hello World! Everyone! This Is My First String!'
 >>> s.split()
 ['hello', 'World!', 'Everyone!', 'This', 'Is', 'My', 'First', 'String!']
 >>> s.split(' ',4)
 ['hello', 'World!', 'Everyone!', 'This', 'Is My First String!']
 >>> s.split('e')
 ['h', 'llo World! Ev', 'ryon', '! This Is My First String!']
 >>> s.rsplit()
 ['hello', 'World!', 'Everyone!', 'This', 'Is', 'My', 'First', 'String!']
 >>> s.rsplit(' ',4)
 ['hello World! Everyone! This', 'Is', 'My', 'First', 'String!']
 >>> s.rsplit('e')
 ['h', 'llo World! Ev', 'ryon', '! This Is My First String!']
#s.split([sep, [maxsplit]]) 以sep是分隔符,把s分割成一个list。sep默认为空格。maxsplit是分割的次数,默认是对整个s进行分割
#s.rsplit([sep, [maxsplit]])  和split()的区别是它是从s的串尾往前进行分割


 >>> s=s.replace(' ','/n')
 >>> s
 'hello/nWorld!/nEveryone!/nThis/nIs/nMy/nFirst/nString!'
 >>> s.splitlines()
 ['hello', 'World!', 'Everyone!', 'This', 'Is', 'My', 'First', 'String!']
 >>> s.splitlines(True)
 ['hello/n', 'World!/n', 'Everyone!/n', 'This/n', 'Is/n', 'My/n', 'First/n', 'String!']
 >>> s.splitlines(False)
 ['hello', 'World!', 'Everyone!', 'This', 'Is', 'My', 'First', 'String!']
 >>> '/t'.join(s.splitlines())
 'hello/tWorld!/tEveryone!/tThis/tIs/tMy/tFirst/tString!'
#s.splitlines([keepends]) 把s按照行分隔符分成一个list。如果keepends为True则list的每个元素保留行分割符,如果为False则不保留分隔符
#s.join(seq)   用s把seq序列串联起来

 

字符串的测试函数,这一类函数在string模块中没有,这些函数返回的都是bool值
 >>> s
 'hello/nWorld!/nEveryone!/nThis/nIs/nMy/nFirst/nString!'
 >>> s.startswith('hello')
 True
 >>> s.startswith('W',6)
 True
 >>> s.endswith('!')
 True
 >>> s.endswith('g!')
 True
 >>> s.endswith('r',0,s.rindex('r')+1)
 True
#s.startwith(prefix[,start[,end]]) s是否在[start:end]内以prefix开头,是返回True,否返回False
#s.endswith(suffix[, start[, end]]) s是否在[start:end]内以suffix结尾,是返回True,否返回False

 >>> s='hello world123'
 >>> s.isalnum(), s.replace(' ','').isalnum() 
 (False, True)
#是否全是字母和数字,并至少有一个字符 
 >>> s.isalpha(),s.replace(' ','').replace('123','').isalpha()
 (False, True)
#是否全是字母,并至少有一个字符
 >>> s.isdigit(),s.replace('hello world','').isdigit()
 (False, True)
#是否全是数字,并至少有一个字符
 >>> s.isspace(),s.replace('hello','').replace('world123','').isspace()
 (False, True)
#是否全是空白字符,并至少有一个字符
 >>> s.islower(),s.upper().islower(),s.isupper(),s.upper().isupper()
 (True, False, False, True)
#s中的字母是否全是小写(islower())/大写(isupper())
 >>> s.istitle(), s.capitalize().istitle(), s.title().istitle()
 (False, False, True)
#s中的单词是否全部是首字母大写的

参考http://bbsunchen.javaeye.com/blog/552013 的介绍,自己做了一些小实验。分享如下,大家一起学习。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics