python字符串替换2种方法:
1、用字符串本身的方法repalce替换。
2、正则模块re模块替换字符串。
例子:
a = 'hello word'
把a字符串里的word替换为python
1、用字符串本身的replace方法
 
复制代码 代码示例:
a.replace('word','python')
输出结果:
hello python
2、用正则表达式来完成替换:
 
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
输出结果:
hello python
php5.2升级到5.3后,原& new的写法已经被放弃了,可以直接new了。
python文件扩展名修改脚本:
 
复制代码 代码示例:
#!/usr/bin/env python
#-*- coding:utf-8 -*- 
#!/usr/bin/python  
import os
#定义程序根目录
rootpath='D:wampwwwerpapp'
def m_replace(path):
 for item in os.listdir(path):
  nowpath=os.path.join(path,item)
  if os.path.isdir(nowpath):
   m_replace(nowpath)
  else:
   if nowpath.find('.php')>0:
    f=open(nowpath,'r+')
    content=f.read().replace('& new ','new ')
    open(nowpath,'w').write(str(content))
    f.close()
if __name__=="__main__":
 m_replace(rootpath)