Python text replace with for loop

Python text replace with for loop



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





Why do you overwrite the file at each iteration?
– DYZ
Aug 30 at 2:15






i am a newbie is there a better way ?
– San Diego
Aug 30 at 2:18





If your file is all times, and you want to remove them, why not just delete the file. If your file is not all datetimes, please provide an example to show what the other data looks like, in other words, an Minimal, Complete, and Verifiable example
– user3483203
Aug 30 at 2:18






Need more information. The output shown is what you expected or the result of your code? Are you trying to remove the hours and minutes?
– MichaelR
Aug 30 at 2:21





What are trying to accomplish?
– tpk
Aug 30 at 2:24




1 Answer
1



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)





It really worked. Thank you
– San Diego
Aug 30 at 22:21



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages