let's say I have a file Example.txt
like this:
alpha_1 = 10
%alpha_2 = 20
Now, I'd like to have a python script which performs these tasks:
If the line containing
alpha_1
parameter is not commented (%
symbol), to rewrite the line adding%
, like it is withalpha_2
To perform the task in 1. independently of the line order
To leave untouched the rest of the file
Example.txt
The file I wrote is:
with open('Example.txt', 'r+') as config:
while 1:
line = config.readline()
if not line:
break
# remove line returns
line = line.strip('\r\n')
# make sure it has useful data
if (not "=" in line) or (line[0] == '%'):
continue
# split across equal sign
line = line.split("=",1)
this_param = line[0].strip()
this_value = line[1].strip()
for case in switch(this_param):
if case("alpha1"):
string = ('% alpha1 =', this_value )
s = str(string)
config.write(s)
Up to now the output is the same Example.txt
with a further line (%alpha1 =, 10)
down the original line alpha1 = 10
.
Thanks everybody