I am trying to search through a .c script and replace several strings with a float. It’s easy to do when I only want to replace one string. Problem is I have several different strings that all appear once, that need to be replaced by different information.
I believe my mistake is in the “for lines in file in”. Every time I add a new line in this loop where I try to replace a string, it adds an extra duplicate line to my code. I am struggling to find a way to replace all the strings in the one loop.
Here is the part of my code where I am having problems:
with open(input_file, 'r') as filein:
with open(MATERIAL_LIST[number]+'.c', 'w+') as fileout:
for line in filein:
fileout.write(line.replace('TBOIL_VAL', str(BOILING_POINT[number])))
fileout.write(line.replace('THERMAL_CONDUCTIVITY_VAL', str(THERMAL_CONDUCTIVITY[number])))
fileout.close()
filein.close()
Thanks for your time
I tried to look at each line in my filein, and replace strings with different floats as my fileout. I can successfully do this with one string replacement, but anything over one and duplicates come into play.
senatorarmstrong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.