This is my program:
filetest = open ("testingfile.txt", "r+")
ID = input ("Enter ID: ")
list = ['Hello', 'Testing', 'File', 543210]
for line in filetest:
line = line.rstrip ()
if not ID in line:
continue
else:
templine = '\t'.join(map(str, list))
filetest.write (line.replace(line, templine))
filetest.close ()
I'm trying to replace an entire line containing the ID entered in filetest with templine (templine was a list joined into a string with tabs), and I think the problem with my code is specifically this part filetest.write (line.replace(line, templine)), because when I run the program, the line in the file containing ID doesn't get replaced, but rather templine is added to the end of the line.
For example, if the line in filetest found using the ID entered was "Goodbye\tpython," now it becomes "Goodbye\tpythonHello\tTesting\tFile\t543210" which is not what I want. How do I ensure that line "Goodbye\tpython" is replaced by templine "Hello\tTesting\tFile\t543210", not appended?
filetest.write(templine)?