在Python脚本中使用regex

2024-05-14 10:47:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个python脚本来搜索和替换此行:

剩余时间3总计

空行

这是我的剧本:

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)

使用这个,我无法得到任何更换。为什么会这样?其余的代码工作正常。输出文件没有更改,所以我怀疑找不到模式

谢谢你


Tags: txt脚本readaswithfilesopenout

热门问题