+ 1
Find and replace a sentence in a file
Suppose I have to find a sentence which contains space like "I am previous sentence" in a text file and replace with "I am replaced string"
4 Réponses
+ 1
Finally ,i got the code.
Link:https://code.sololearn.com/c5rYQJ6qUbr1
+ 4
Is each sentence one line?
Means the file is
I am a nerd.
I am cool.
I am great.
Or is file
I am a nerd. I am cool. I am great.
+ 1
This should work fine
import re
with open('/storage/emulated/0/replace_spaces.py','r+')as f:
a=[]
str=f.read().split(".")
for i in str:
if re.search(" {2,}",i):
a.append("something else")
else:
a.append(i)
f.seek(0)
f.write(".".join(a))
f.truncate()
if you have any question related to code let me know
0
like this : I am a nerd. I am cool. I am great.
Frogged