python 字符串与正则表达式
2012-09-03 10:50
357 查看
s.find(sub,start=0,end=sys.maxint) if it haven't find the substring ,return -1
Bool s.isdigit(), if len(s)==0 then return False
Bool s.isalnum()s.isalpha(),s.islower(),s.isspace(),s.isupper()
s.join(seq) for instance :'abc'.join('123') --> '1abc2abc'
s.split() 'a b'.split()-->['a','b'] 'a b'.split('')-->['a','','b']
maketrans(str1,str2)将str1中的字母翻译成str2中,一一对应,translate(table,deletechars='')其中的table就可以用maketrans产生的,这个函数将把deletechars的字符删掉,然后按照table的规则进行转换。
for instance
import string
intoas=string.maketrans('eiou','aaaa') -->一定是一一对应的
print 'somestring.'.translate(intoas) -->sama strang
print 'somestring.'.translate(intoas,'s') ->ama trang
精度转换 %d %i 是输出带符号的整数
而%f %F float %e%E是用指数形式表示的浮点数
%g %G是在exp>=4 orexp<=精度时候,与e或E一样,否则和F or f一样
import codecs
#open(filename,mode='rb',encoding=None,errors='strict',buffering=1)
fileout = codecs.open('uni.txt','w','latin-1','replace')
fileout.write(u'gauss')将unicde字符串写入到文件uni.txt中,并让字符串在文件中被隐式编码为latin-1,同时,在对任何不能以latin-1编码的字符,将其替换为‘?’,如果errors是ignore,转换工作将跳过错误,还有是xmlcharrefreplace,
buffering :1写入缓存中,通过close()存入硬盘,而buffering=0 将直接对硬盘操作
EncodedFile(file,datacodec,filecodec=None,errors='strict')
包装类文件对象file,返回一个类文件对象ef,在向ef写入一个str时候,ef将首先使用datacodec解码,然后用filecodec编码在写入file中,从ef读取一个字符串时候,就相反,
例如,用latin-1编码的字符串写入到sys.stdout,并且输出编码方式为utf8的字符串,
import sys,codecs
sys.stdout = codecs.EnocdedFile(sys.stdout,'latin-1','utf-8')
相关文章推荐
- python利用正则表达式提取字符串
- Python 字符串和正则表达式的使用
- 1900页Python系列PPT分享四:字符串与正则表达式(109页)
- python 正则表达式匹配字符串末尾的错误
- [python]关于字符串查找和re正则表达式的效率对比
- Python正则表达式如何进行字符串替换
- Python 正则表达式匹配字符串中的日期和时间
- python 学习记录(10)-字符串与正则表达式
- Python 正则表达式验证字符串
- 在python中使用正则表达式查找可嵌套字符串组
- Python判断字符串是否符合某一正则表达式
- Python 3(4)数据类型:字符串 & 正则表达式
- Python正则表达式如何进行字符串替换
- 用python3.x正则表达式匹配中文字符串
- Learning Python 008 正则表达式-007 匹配的字符串模板中如果只有前面有字符串,而后面没有字符串时,这个匹配模板要怎 么写
- python字符串及正则表达式[转]
- Python正则表达式如何进行字符串替换
- Python通过正则表达式和字符串处理获取方式获取所需子字符串的方式
- python正则表达式获取两段标记内的字符串
- python3 shell 正则表达式 攫取复杂字符串特定子串