Python替换文件内容

文件部分内容如下:

    <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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值