Python文本文件内容修改实例(支持正则表达式)

因项目上经常需修改大量脚本配置内容,故写了此脚本进行批量精确替换修改配置文件。

引用模块:

import  re#用于从字符串中提取数据
import io#解决低版本Python 2.7.5的异常(TypeError: 'encoding' is an invalid keyword argument for this function)

简易实例:

#coding:utf-8#防止文件编码乱码
###################################################
#=============================   加载对应模块   ===========================================#
import  re#用于从字符串中提取数据
import io#解决低版本Python 2.7.5的异常(TypeError: 'encoding' is an invalid keyword argument for this function)

##################################################################################
#=============================   def功能定义   ===========================================#
# 设置默认读写的文件编码
encoding1='utf-8'
# 对配置文件进行替换
#-----
# 打开文件,读取文件内容,然后关闭文件,输出内容至全局变量file_b
def file_a(path_file,path_file_0or1):
    # 打开文件,读取文件内容,然后关闭文件
    path_file = str(path_file)#文件路径
    global file_b
    temp = io.open(path_file, 'r', encoding=encoding1)
    file_b = temp.read()
    temp.close()
    if path_file_0or1 == 1:
        print(file_b)
    return
#________________________________________________________________________________________
#-----
# 打开文件,写入文件内容,然后关闭文件(参数A:写入的内容,参数B:写入的路径)
def file_write(final_scores,path_file):
    # 打开文件,写入文件内容,然后关闭文件
    temp = io.open(path_file,'w',encoding=encoding1) 
    temp.writelines(final_scores)
    temp.close()

##################################################################################
#=============================   start1   ===========================================#
encoding1='utf-8'#读写文件时采用的编码格式
temp_file1="app.js"
print("尝试以%s的编码格式打开文件%s" % (encoding1,temp_file1))
# 打开文件,获取文件内容至变量中。
file_a(temp_file1,0)#可选参数:1,输出文件内容,0,不输出文件内容。
# 执行替换操作
temp = (re.sub(r'11','22',file_b))#查找文件中字串符 11,将其替换为 22
temp = (re.sub(r'privateKey=.*','MIICdgIBADANBgkq',temp))#使用正则表达式查找包含字串符 privateKey= 的行,并替换为 privateKey='MIICdgIBADANBgkq'
#注意:上方第二行开始,传参对象已变成了temp。
print("替换完成,将结果保存至文件中")
# 将结果写入文件
file_write(temp, temp_file1)
print("已更新:%s" %temp_file1)
print('如果需要替换多个文件,直接复用start1的调用,更改关键参数即可。')

#=============================   start2   ===========================================#
#encoding1='utf-8'#读写文件时采用的编码格式
#temp_file1="start_app.js"
#print("尝试以%s的编码格式打开文件%s" % (encoding1,temp_file1))
# 打开文件,获取文件内容至变量中。
#file_a(temp_file1,0)#可选参数:1,输出文件内容,0,不输出文件内容。
# 执行替换操作
#temp = (re.sub(r'111','222',file_b))#查找文件中字串符 111,将其替换为 222
#temp = (re.sub(r'privateKey1=.*','MIICdgIBADANBgkq1',temp))#使用正则表达式查找包含字串符 privateKey=1 的行,并替换为 privateKey='MIICdgIBADANBgkq1'
#注意:上方第二行开始,传参对象已变成了temp。
#print("替换完成,将结果保存至文件中")
# 将结果写入文件
#file_write(temp, temp_file1)








进阶实例:【已去除关键信息】

#coding:utf-8

###########################################################################################

#=================配置文件目录配置=================#

#前端、后端、工作流配置文件路径:

#前端qd.js

File_dist_qd=str('qd.js')

#后端hd.yml

File_root_hd=str('hd.yml')

#=================IP地址配置=================#

#OAip

OA_ip="127.0.0.1"

#OA端口

OA_prot="9999"

#=================数据库配置=================#

#数据库连接池设置

initialSize="30"# 初始连接数

minIdle="30"# 最小连接池数量

maxActive="300"# 最大连接池数量

#=================秘钥配置=================#

#公钥

publicKey="我是公钥"

#私钥

privateKey="我是私钥"

#密文

password="我是密文"

#--输出秘钥内容

print("公钥内容:%s"%publicKey)

print("私钥内容:%s"%privateKey)

print("密文内容:%s"%password)

###########################################################################################

#=============================   加载对应模块   ===========================================#

import  re#用于从字符串中提取数据

import io#解决低版本Python 2.7.5的异常(TypeError: 'encoding' is an invalid keyword argument for this function)

###########################################################################################

#=============================   def功能定义   ===========================================#

# 对配置文件进行替换

# 打开文件,读取文件内容,然后关闭文件,输出内容至全局变量file_b

def file_a(path_file,path_file_0or1):

    # 打开文件,读取文件内容,然后关闭文件

    path_file = str(path_file)#文件路径

    global file_b

    temp = io.open(path_file, 'r', encoding='utf-8')

    file_b = temp.read()

    temp.close()

    if path_file_0or1 == 1:

        print(file_b)

    return

#__________________________________________________________________________________________

# 打开文件,写入文件内容,然后关闭文件(参数A:写入的内容,参数B:写入的路径)

def file_write(final_scores,path_file):

    # 打开文件,写入文件内容,然后关闭文件

    temp = io.open(path_file,'w',encoding='utf-8')

    temp.writelines(final_scores)

    temp.close()

###########################################################################################

#=============================   秘钥文件替换   ===========================================#

###########################################################################################

#=================后端 File_root_hd=================#

# 秘钥文件替换

file_a(File_root_hd,0)#可选参数:1,输出文件内容,0,不输出文件内容。

# 秘钥文件替换

temp = (re.sub(r'privateKey:.*',"privateKey: "+privateKey,file_b))

# 将结果写入文件

file_write(temp, File_root_hd)

print("已将秘钥内容写入文件:%s" %File_root_hd)

###########################################################################################

#=============================   IP地址替换   ============================================#

###########################################################################################

print("开始进行IP地址替换:%s" %File_dist_qd)

#=================前端 File_dist_qd =================#

# qd.js

file_a(File_dist_qd,0)#可选参数:1,输出文件内容,0,不输出文件内容。

# 内容替换

temp = (re.sub(r'const ip.*','const ip = "'+'http://'+OA_ip+':'+OA_prot+'/app";',file_b))#ip1

# 将结果写入文件

file_write(temp, File_dist_qd)

print("已更新:%s" %File_dist_qd)

#=================后端 File_root_hd =================#

# hd.yml

file_a(File_root_hd,0)#可选参数:1,输出文件内容,0,不输出文件内容。

# 内容替换

temp = (re.sub(r'const ip2.*','const ip2 = "'+'http://'+OA_ip+':'+OA_prot+'/apb";',file_b))#ip2

temp = (re.sub(r'ipp: .*','ipp: '+"http://"+myflowable_ip+":"+myflowable_prot+"/ppppp",temp))#ip3

# 将结果写入文件

file_write(temp, File_root_hd)

print("已更新:%s" %File_root_hd)


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值