【发布时间】:2015-01-29 05:34:38
【问题描述】:
我对 Python 完全陌生,这是我第一个替换 word 的脚本。
我的文件test.c 包含以下两行
printf("\nReboot not supported. Exiting instead.\n");
fprintf(stderr, "FATAL: operation not supported!\n");
现在我想将printf 和fprintf 分别替换为//printf 和//fprintf。
这是我尝试过的
infile = open('path\to\input\test.c')
outfile = open('path\to\output\test.c', 'w')
replacements = {'printf':'//printf', 'fprintf':'//fprintf'}
for line in infile:
for src, target in replacements.iteritems():
line = line.replace(src, target)
outfile.write(line)
infile.close()
outfile.close()
但是用这个我得到了
fprintf 到 //f//printf 这是错误的。
对于解决方案,已查看此 answer 但无法将其放入我的脚本中。
有人知道我该如何解决吗?
【问题讨论】:
标签: python