文件部分内容如下:
<mapping cell="A1">request.aaPriceChangeDesc</mapping>
<mapping cell="B1">request.aaStartDate</mapping>
<mapping cell="C1">request.aaSumCode</mapping>
<mapping cell="D1">request.aaSumType</mapping>
<mapping cell="E1">request.aaItemCode</mapping>
需要将上面的xml文件,A1换成A2,B1换成B2,C1换成C2,以此类推。
import re,os,sys
path=sys.path[0]
def del_file(path):
ls = os.listdir(path)
for i in ls:
c_path = os.path.join(path, i)
if os.path.splitext(c_path)[1] == '.xml':
f = open(c_path,'r')
alllines = f.readlines()
f.close()
f = open(c_path,'wb')
for line in alllines:
for i in range(ord("A"),ord("Z")+1):
if re.search('cell=\"'+chr(i)+'2\"', line):
line = line.replace('cell=\"'+chr(i)+'2\"','cell=\"'+chr(i)+'3\"')
f.write(line.encode())
f.close()
del_file(path)
os.remove(path+"/replace.py")
判断文件后缀名是否是xml:os.path.splitext(c_path)[1] == '.xml'
循环遍历A-Z:for i in range(ord("A"),ord("Z")+1)
执行完后删除文件:os.remove(path+"/replace.py")