Ive got this code from replacing text in a file with Python
import re
repldict = {'zero':'0', 'one':'1' ,'temp':'bob','garage':'nothing'}
def replfunc(match):
return repldict[match.group(0)]
regex = re.compile('|'.join(re.escape(x) for x in repldict))
with open('file.txt') as fin, open('fout.txt','w') as fout:
for line in fin:
fout.write(regex.sub(replfunc,line))
but i cant load the repldict from other txt file. i've tried with
repldict={line.strip() for line in open(syspath+'/rules.txt', 'r')}
But it doesn't work.