D
DataSmash
Hello,
I need to search and replace 4 words in a text file.
Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.
Can someone help me out.
Thanks!
# Search & Replace
file = open("text.txt", "r")
text = file.read()
file.close()
file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()
I need to search and replace 4 words in a text file.
Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.
Can someone help me out.
Thanks!
# Search & Replace
file = open("text.txt", "r")
text = file.read()
file.close()
file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()