我正在尝试编写一个python脚本来搜索和替换此行:
空行
这是我的剧本:
import glob
read_files = glob.glob("*.agr")
with open("out.txt", "w") as outfile:
for f in read_files:
with open(f, "r") as infile:
outfile.write(infile.read())
with open("out.txt", "r") as file:
filedata = file.read()
filedata = filedata.replace(r'#time\s+residue\s+[0-9]\s+Total', 'x')
with open("out.txt", "w") as file:
file.write(filedata)
使用这个,我无法得到任何更换。为什么会这样?其余的代码工作正常。输出文件没有更改,所以我怀疑找不到模式
谢谢你
str.replace
方法替换固定的子字符串。如果要替换正则表达式模式的匹配项,请使用re.sub
:相关问题 更多 >
编程相关推荐