python re 與 re.sub替換部分文件
阿新 • • 發佈:2018-07-06
user line 配置 config readlines python re lua http mongod 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‘)
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():
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)
python re 與 re.sub替換部分文件