[python-win32] How to replace the values with keys ?
Tim Roberts
timr at probo.com
Thu Jul 24 18:16:55 CEST 2008
Graps Graps wrote:
>
> aDict is a dictionary, containing
>
> {0: 'str1\n', 1: 'str22\n', 2: 'str3\n', 3: 'str4\n', 4: 'str5\n',
> ..........., , 1308: 'str1309\n'}.
Are you saying that string is exactly what is in the file "aDict.py"?
In that case, you can't import it. The expression will be evaluated,
and then discarded. You would have to read it as a string and use "eval":
aDict = eval( open( 'aDict.py', 'r' ).read() )
However, because that's dangerous, I strongly recommend that you change
aDict.py so that it reads:
aDict = {0: 'str1\n', 1: 'str2\n', ... }
That way, you COULD use
from aDict import aDict
> 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.
OK, then why use a dictionary at all? If the indices are just
sequential numbers, why not just make it a list:
aList = [ 'str1\n', 'str2\n', 'str3\n', 'str4\n' ... ]
You would use it exactly the same way:
from aDict import aList
...
newValue = aList[ i ]
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32
mailing list