Python文本替换为for循环

Python文本替换为for循环,python,python-3.x,Python,Python 3.x,我只是想删除所有的datetime值。。。 但每次它都会转到开头,并用最后一个值刷新文本。如何删除所有时间值 我的文本文件是hey.txt,这是它的内部: 14:15 24:32 trying 42:56 1:42 for testing 代码部分: import re filename = "hey.txt" text = open(filename).read() n,i,c,x,=(0,0,0,0) datetimes = [] for number in range(3600):

我只是想删除所有的datetime值。。。 但每次它都会转到开头,并用最后一个值刷新文本。如何删除所有时间值

我的文本文件是hey.txt,这是它的内部:

14:15
24:32
trying
42:56
1:42
for
testing
代码部分:

import re
filename = "hey.txt"
text = open(filename).read()
n,i,c,x,=(0,0,0,0)
datetimes = []
for number in range(3600):
    n+=1
    b=str(n)
    if n<10:
        b = "0"+str(n)
    elif n==60:
        i+=1
        n=0 
    datetimes.append("%d:%s"%(i,b)) 
for word in datetimes:    
    matches = re.compile(word).finditer(text)
    for match in matches:
        z= match.group(0)
        print(z)
        if z==word:
            open("datetimes-doc.txt","w+").write(text.replace(z,""))
            c+=1
        elif z!=word:
            c+=1
        print("proccess:%d"%c)
以下是控制台的一些输出:

1:42
proccess:1
2:56
proccess:2
4:15
proccess:3
4:32
proccess:4
14:15
proccess:5
24:32
proccess:6
42:56
proccess:7

与其说是for循环,不如说:

#read entire file's content 
with open('hey.txt') as f:
    content = f.read()

# find time and replace with empty string
new_content = re.sub(r'[0-5]*[0-9]:[0-5]*[0-9]\n','', content)

#write results into file
with open('hey2.txt', 'w') as f:
    f.write(new_content)

为什么每次迭代都要覆盖文件?我是新手,有没有更好的方法?如果你的文件一直都是,并且你想删除它们,为什么不直接删除文件呢。如果您的文件不是所有日期时间,请提供一个示例来说明其他数据的外观,换句话说,您需要更多信息。显示的输出是您期望的结果还是代码的结果?您是否试图删除小时和分钟?您试图完成什么?
#read entire file's content 
with open('hey.txt') as f:
    content = f.read()

# find time and replace with empty string
new_content = re.sub(r'[0-5]*[0-9]:[0-5]*[0-9]\n','', content)

#write results into file
with open('hey2.txt', 'w') as f:
    f.write(new_content)