python查找替换
场景:
系统有些配置文件,在不同环境下需要对部份配置项进行查找替换,如下的工作就是要找到这些需要改变其值的关键字(即key/value中的key).
假设有文件:I:\python\1.txt,内容如下:
path=/home/pos.propertes user=ds_user passwd=passwd_db
现在需要找到等号=前面的关键字,实现代码如下:
代码实现:
# encoding: UTF-8 import re import os fd=open(r'I:\python\1.txt') result = fd.read() fd.close #print result #print "-------- end -----------" p = re.compile("(.*)=") print p.findall(result)
#------- 结果 --------
['path', 'user', 'passwd']
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有