I want to replace value of key(i.e db_host
, addons_path
) with $$$$
.
Input text file contains the following:
#Test.txt#
addons_path=/bin/root
admin_passwd = abctest
auto_reload = False
csv_internal_sep = ,
db_host = 90.0.0.1
Output text file:
#Test2.txt#
admin_passwd = abctest
auto_reload = False
csv_internal_sep = ,
db_host = $$$$$
I want to replace value of particular key and write it in a file, than replace old file with new file.
The following function gives me correct output of replacing value of particular key import fileinput from pprint import pprint as p
replace_with = '7777'
key = 'db_host'
fileref = open('/Files/replace_key/test','r+')
line = fileref.readline()
config = []
while line:
split_line = line.split('=')
if len(split_line ) == 2:
config.append( ( split_line[0].strip(' \n'),split_line[1].strip(' \n') ) )
print line
line = fileref.readline()
fileref.close()
config = dict(config)
print config
config.update({'db_host':replace_with})
p(config)
But I am unable apply it to entire text file.