[python-win32] How to replace the values with keys ?

Graps Graps wegraps at gmail.com
Thu Jul 24 04:02:58 CEST 2008


Hi all,


aDict is a dictionary, containing

{0: 'str1\n', 1: 'str22\n', 2: 'str3\n', 3: 'str4\n', 4: 'str5\n',
..........., , 1308: 'str1309\n'}.

I used:

keys = m
values = noDupes
aDict = dict(zip(keys,values))

to get aDict, where m=range(1309) and noDupes was a list read from a text
file.

When I tried:

from aDict import aDict

I am thrown with:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name aDict.

Thanking You in advance.




On Wed, Jul 23, 2008 at 11:03 PM, Tim Roberts <timr at probo.com> wrote:

> Graps Graps wrote:
>
>> Hi all,
>>
>> I tried
>>
>> from aDict 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.
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20080724/b468e0d2/attachment.htm>


More information about the python-win32 mailing list