Here is a code which shows how to find and replace multiple text in a file using Python:
search_text_1 = "36419"
search_text_2 = "250847"
search_text_3 = "2764437"
search_text_4 = "OOGGT"
i=1
for j in range(2000):
with open('C:\\Users\\Original\\'
'source_filename.txt', 'r') as file:
data = file.read()
GS = int(search_text_1) + i
data1 = data.replace(search_text_1, str(GS))
with open('C:\\Users\\Original\\Champ\\'
'E_EI4.20213.024.35.X1.243_' + str(i) + '.txt', 'w') as file:
file.write(data1)
with open('C:\\Users\\Original\\Champ\\'
'E_EI4.20213.024.35.X1.243_' + str(i) + '.txt', 'r') as file:
data = file.read()
N9 = int(search_text_2) + i
data2 = data.replace(search_text_2, str(N9))
with open('C:\\Users\\Original\\Champ\\'
'E_EI4.20213.024.35.X1.243_' + str(i) + '.txt', 'w') as file:
file.write(data2)
with open('C:\\Users\\Original\\Champ\\'
'E_EI4.20213.024.35.X1.243_' + str(i) + '.txt', 'r') as file:
data = file.read()
BN = int(search_text_3) + i
data3 = data.replace(search_text_3, str(BN))
with open('C:\\Users\\Original\\Champ\\'
'E_EI4.20213.024.35.X1.243_' + str(i) + '.txt', 'w') as file:
file.write(data3)
with open('C:\\Users\\Original\\Champ\\'
'E_EI4.20213.024.35.X1.243_' + str(i) + '.txt', 'r') as file:
data = file.read()
# BN = int(search_text_3) + i
data4 = data.replace(search_text_4, "hey!")
with open('C:\\Users\\Original\\Champ\\'
'E_EI4.20213.024.35.X1.243_' + str(i) + '.txt', 'w') as file:
file.write(data4)
print("hey! -> destination file file is created. Count is:", i)
i=i+1
print("hey! -> destination file test Data is generated....")
Comments
Post a Comment