I am looking at replacing multiple strings with input from the user. The following code (which is a modification of a code from one of the queries here in stackoverflow; pardon coz I can't find the thread anymore) works well when finding and replacing one instance(done on purpose) of a specified string:
print('What word should we replace s1 with?')
input_1 = input()
with open('C:\\dummy1.txt')as f:
sample1 = f.read().replace("s1", str(input_1), 1)
with open('C:\\dummy2.txt',"w") as f1:
f1.write(sample1)
Now when I try copy-pasting the same and modifying it to cater to other strings, only the last specified string gets replaced... here's a sample code:
print('What word should we replace s1 with?')
input_1 = input()
with open('C:\\dummy1.txt')as f:
sample1 = f.read().replace("s1", str(input_1), 1)
with open('C:\\dummy2.txt',"w") as f1:
f1.write(sample1)
print('What word should we replace s2 with?')
input_2 = input()
with open('C:\\dummy1.txt')as f:
sample2 = f.read().replace("s2", str(input_2), 1)
with open('C:\\dummy2.txt',"w") as f1:
f1.write(sample2)
What do I need to do to make this work seamlessly for multiple strings? Please consider explaining it to someone with less than a year experience in coding and 9-hour worth of video learning in python :) Thank you!