I searched for an answer to my question on this site. There are answered questions like:
but I'm not able to transform the information to my problem, so:
I need to replace a specific value in a text file. The file structure looks like this:
black
{
part cone;
value 20;
}
yellow
{
part wing;
value 30;
}
In a GUI the user defines the color and the new value. For example: black = 30
. If the user starts the script, the input values are a="black"
and b=30
.
My piece of code:
dpd=open("colors","r+")
ft=dpd.read()
ft_new=re.sub(???,???,ft)
dpd.seek(0)
dpd.write(ft_new)
dpd.truncate()
dpd.close()
I tried something like this:
re.sub(str(a)+'\n.*\n.*\n.*value [0-9][0-9]?',str(a)+'\n.*\n.*\n.*?value '+str(b),ft)
but this does not work. I understand how to substitute a single string but I don't get how to substitute a specific value in a specific place in the file.