NameError when assigning dictionary values

Darrell darrell at dorb.com
Mon Apr 3 01:37:01 EDT 2000


"lewst"
>
> >>> one, two = [], []
>
> >>> mydict = { ".": one, "+": two }
>
> This seems error prone if the the number of variables grows very large
> as it will for me.
>
So you have a large number of things to add to a dictionary an you don't
like the available syntax.

No problem.
Write in what ever syntax you like, then parse it into a syntax Python can
handle.

In a text file:
Key:value
Key:value

Then something like this can read the file into the dictionary.

dic={}
lines=open(theFile).readlines()
for l in lines:
    key, value = string.split(l,':')
    dic[key]=value

For large data sets I've generated the python source instead of this kind of
loop. Then import the resulting file to generate a .pyc file. Then you have
a huge dictionary that is ready to go.

--Darrell








More information about the Python-list mailing list