I have a .wbjn file (ANSYS workbench journal file) which is a python file in which I have to replace certain variables. Eg.
parameter_1 = variable_1
parameter_2 = variable_2
.
.
parameter_n = variable_n
I have to replace the variables_1....n with a list of values.
So I created a mapping dictionary and iterated over it to replace the values. But the values are not getting replaced with the code I wrote;
map_dict = {'variable_1':'20','variable_2':'15'}
handle = open("filename.wbjn","r+")
for l in handle:
for k,v in map_dict.items():
l = l.replace(k,str(v))
handle.write(l)
handle.close()
The file has several other lines which do not need to be modified. What is the problem here and how do I solve it.
Thanks
It looks like, already after reading the first line, it jumps at the end of the fileto write, and after that there is no other line. Mine is not the most elegant solution, but it works:
map_dict = {"variable_1": "20", "variable_2": "15"}
handle = open("filename.wbjn", "r+")
processed = []
for l in handle:
for k, v in map_dict.items():
l = l.replace(k, str(v))
processed.append(l)
handle.seek(0)
handle.write("".join(processed))
handle.truncate()
handle.close()