I want to replace certain lines in my text with another string. So far I was able to locate the text I want to replace with:
text = open('sample','r').read()
regex = re.compile('.*#= min, max\s')
for match in regex.finditer(text):
print match.group(0) #this is what i want to replace
EDIT: also tried
text = open('sample','r').read().split('\n')
for line in text:
line = re.sub('.*#= min, max\s', "HOLA", line)
The text stays the same. Could it be that my regex is messed up? I used the same one somewhere else and there was no problem. Also its a simple regex.
How do I switch it to another line? Thanks!
textstays the same. You iterate it over and modify the variableline, but drop its contents immediately. Trynewtext=[re.sub('.*#= min, max\s', "HOLA", line) for line in text].