[python-win32] How to replace the values with keys ?
Tim Roberts
timr at probo.com
Wed Jul 23 19:33:49 CEST 2008
Graps Graps wrote:
> Hi all,
>
> I tried
>
> import aDict
> import re
> infile = open('text1.txt','rw')
> outfile = open('text3.txt','w')
> def replace_words(infile, aDict):
> rc=re.compile('|'.join(map(re.
> escape, aDict)))
> def translate(match):
> return aDict[match.group(0)]
> return rc.sub(translate, infile)
> outfile = replace_words(infile,aDict)
>
> I am thrown with:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 2, in replace_words
> TypeError: argument 2 to map() must support iteration
>
> I imported text2.txt , containing python dictionary as aDict.py. I
> want the replaced values in a separate file text3.txt.
You need to show us exactly what aDict.py contains. If it looks like this:
aDict = {
'a': '1', ...
}
then you should probably start your program with this:
from aDict import aDict
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32
mailing list