I can use this code below to create a new file with the substitution of a
with aa
using regular expressions.
import re
with open("notes.txt") as text:
new_text = re.sub("a", "aa", text.read())
with open("notes2.txt", "w") as result:
result.write(new_text)
I was wondering do I have to use this line, new_text = re.sub("a", "aa", text.read())
, multiple times but substitute the string for others letters that I want to change in order to change more than one letter in my text?
That is, so a
-->aa
,b
--> bb
and c
--> cc
.
So I have to write that line for all the letters I want to change or is there an easier way. Perhaps to create a "dictionary" of translations. Should I put those letters into an array? I'm not sure how to call on them if I do.