簡體   English   中英

遍歷文件

[英]iterate over file

我想遍歷文件並刪除與給定正則表達式匹配的一系列行。 我有下面的腳本,但它只刪除了第一個匹配的行/正則表達式。 我如何遍歷文件才能使其正常工作?

import glob
import re

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 = re.sub(r'#time\s+residue\s+[0-9]\s+Total', '', filedata)
with open("out.txt", "w") as file:
    file.write(filedata)

謝謝

我解決了這個問題,我需要修改正則表達式以指定> = 1位,如下所示:#time \\ s + residue \\ s + [0-9] + \\ s + Total。 以前,正則表達式中沒有+號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM