Aug-06-2021, 10:00 PM
(Aug-06-2021, 09:36 PM)bowlofred Wrote:(Aug-06-2021, 09:13 PM)AlphaInc Wrote: Where do I have to define that my script should check in text-files?
In my example, the text is already in a string calledparagraph
. In your example, you read in the text file to a variable calledfiledata
. They're both just strings. Rename them how you want.
Quote:And what's the difference between target and paragraph?
One is what you're looking to replace, one is where you're looking and replacing it. Did you try running the code?
Yeah I did but it did not change my text-file. I changed it a little bit to this.
import re target = "example" def str_counter(match_object): str_counter.count += 1 return str(str_counter.count) str_counter.count = 0 with open('text.txt', 'r') as file : filedata = file.read() filedata = re.sub(re.escape(target), str_counter, filedata) print(filedata)This works fine except for one last thing - how can I save the printed output to the same file and overwrite it?