Here is a problem.
I have a .py file and a .txt.
To simplify, my .txt looks like:
@x@
In the .py I have
x=15
I would like to replace @x@ in the txt by the value saved in the py, ie my txt should look like
15
I tried with this:
for i, line in enumerate(fileinput.input('mytxtfile.txt', inplace = 1)):
sys.stdout.write(line.replace('@x@', 'x'))
or with
for i, line in enumerate(fileinput.input('mytxtfile.txt', inplace = 1)):
sys.stdout.write(line.replace('@x@', 'str(x)'))
The problem is that the “replace” method seems to consider only strings and I need to evaluate the value of the string.
Any idea how to do it?
Thanks
just leave the quotes off
str(x)If you want more control over the output you can use a format string. Eg for 2 decimal places