define variables in the txt file using python

dieter dieter at handshake.de
Sat Oct 8 02:40:04 EDT 2016


Xristos Xristoou <saxri89 at gmail.com> writes:
> i have one .txt file and i want to create python script with sys.argv or argparse or other package to define some variables in the txt file and i take some result.
>
> txt file maybe like this :
>
> input number 1= %var1%
> input number 2= %var2%
> result = %vresult(var1-var2)%
>
> how can i write a python script to update dynamic variables in the txt file ?
> i dont want with replace i thing so arguments is the better.

You read the file in; you look for things in the resulting string
with features you find in the "re" (= "Regular Expression") module
and maybe replace them; you write the result out.

Things like "%vresult(var1-var2)%" may be a bit more difficult.
You might need to use a parser library to get the expressions parsed
and then evaluate it with your own Python code. Python by itself
does not include a general parser library (only one for Python source code),
but there are a few available for Pytin in third party packages.




More information about the Python-list mailing list