The general idea is to store the replacement values in a container like a dictionary, isolate the string that you want to test, and replace it if it is in the replacement dictionary. As stated above you could store them in a separate text file and read into a dictionary. The following code uses a list for input and output to simulate files, but gives the general idea.
class Param(object):
def __init__(self):
## parameter dictionary with the key=string to search for,
## and value=replacement value
self.params_dic={"param1":"3",
"param2":"2",
"param3":"1" }
def process_file(self, data_in):
output_list = []
for rec in data_in:
r_list = rec.split("=")
test_s = r_list[1].strip()
if test_s in self.params_dic: ## found
output_list.append("%s=%s\n" % \
(r_list[0], self.params_dic[test_s]))
else: ## not found
output_list.append(rec)
print "final output ="
for rec in output_list:
print rec,
P = Param()
input_file_list = ['a =1\n', 'b=2\n', 'c= param1\n', 'd=4\n', 'e=param3\n']
P.process_file(input_file_list)
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
Is this possible? The original text file will be around 100Mb and have maybe 100 parameters.
Were you able to do this with the suggestions posted?
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714