python re 与 re.sub替换部分文件

inputNum = re.match('(\d+)-(\d+)', userInput)
inputOnenum = re.match('(^[1-9][0-9]*$)', userInput)
re.sub模块替换部分配置文件
old = ['zxjRYZGI','20003','10003','s3','= 3']
new = [password,clientPort,httpPort,dbName,SvrId]
for old,new in zip(old,new):
with open(deploy.servicedir + str(i) + '/Config.lua','r')as f:
for line in f.readlines():
if line.find(str(old)):
line = re.sub(str(old),str(new),line)
configfileLine += line
else:
configfileLine += line
with open(deploy.servicedir + str(i) + '/Config.lua', 'w')as f:
f.write(configfileLine)
configfileLine = ''
with open(passwordPath+'mongo_pass.txt','a+')as f:
mongoDbname = 'bz-tw-and-' + str(i)
f.write(mongoDbname+'='+password+'\n')

re匹配组
\d匹配数字 \w匹配字母
inputNum = re.match('(\d+)-(\d+)', userInput)
inputOnenum = re.match('(^[1-9][0-9]*$)', userInput)

猜你喜欢

转载自blog.51cto.com/yht1990/2137206
今日推荐