+ 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"

24th Oct 2020, 2:21 PM
𝖆𝖙.𝖚𝕷
𝖆𝖙.𝖚𝕷 - avatar
4 Réponses
+ 1
Finally ,i got the code. Link:https://code.sololearn.com/c5rYQJ6qUbr1
24th Oct 2020, 5:11 PM
𝖆𝖙.𝖚𝕷
𝖆𝖙.𝖚𝕷 - avatar
+ 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.
24th Oct 2020, 3:05 PM
Oma Falk
Oma Falk - avatar
+ 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
24th Oct 2020, 4:38 PM
Abhay
Abhay - avatar
0
like this : I am a nerd. I am cool. I am great. Frogged
24th Oct 2020, 3:07 PM
𝖆𝖙.𝖚𝕷
𝖆𝖙.𝖚𝕷 - avatar