Reputation: 81
I am just trying to remove all datetime values... but everytime it is going to the beginning and refreshing the text with the last value. How can i delete the all time values ?
My text file is hey.txt and this is the inside of it:
14:15
24:32
trying
42:56
1:42
for
testing
Code part:
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)
and this is the my sending file datetimes-doc.txt
14:15
24:32
trying
1:42
for
testing
here is the some of output from console:
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
Upvotes: 1
Views: 1103
Reputation: 657
Rather than a for loop, why not:
#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)
Upvotes: 2