subs_names.py
脚本工作都Python的2.6+和Python 3.x的:
#!/usr/bin/env python
from __future__ import print_function
import sys, fileinput
# here goes new values
substitions = dict(TargetName=r"D:\new", FriendlyName="Big")
inplace = '-i' in sys.argv # make substitions inplace
if inplace:
sys.argv.remove('-i')
for line in fileinput.input(inplace=inplace):
name, sep, value = line.partition("=")
if name in substitions:
print(name, sep, substitions[name], sep='')
else:
print(line, end='')
例子:
$ python3.1 subs_names.py input.txt
InstallPrompt=
DisplayLicense=
FinishMessage=
TargetName=D:\new
FriendlyName=Big
如果您满意的输出再加入-i
参数进行更改就地:
$ python3.1 subs_names.py -i input.txt
来源
2010-03-27 00:48:12
jfs
你应该编译的正则表达式代替。 – ghostdog74 2010-03-26 23:55:29
@ ghostdog74:不,regexps是由're'模块自动编译并缓存的。 – nosklo 2010-03-27 00:02:53
我以为只有最近的正则表达式被缓存了 – 2010-03-27 00:18:55