I have a file where I want to replace a string, but all it's doing is appending the end of the file with the replaced string. How can I replace the original occurrences of [NAME] with a string?
Input file
The following are the names of company
[NAME]
[NAME]
[NAME]
Incorporated
When I run my script with a replace I get this.
The following are the names of company
[NAME]
[NAME]
[NAME]
Incorporated
Julie
Stan
Nick
Desired output
The following are the names of company
Julie
Stan
Nick
Incorporated
Python code
output=open("output.txt","r+")
output.seek(0)
name=['Julie', 'Stan', 'Nick']
i=0
for row in output:
if name in row:
output.write(row.replace('[NAME]',name[i]))
i=i+1
print(row)
for row in output:
print(row)
output.close()