[Newbie] How to manipulate variable _names_ ?

Marian Schubert maio at stoupa.sh.cvut.cz
Thu Jun 27 10:05:16 EDT 2002


On Thu, 27 Jun 2002 12:43:01 GMT, Ben Fairbank <baf at texas.net> wrote:
>I have a program that requires the user to enter a file name, such as
>"othello," which the program will then open, process, and create a
>dictionary based on the contents of the file.  I would like to name
>the dictionary "OthelloCount," and then go on and create more
>dictionaries, each with a name made up of the inputfile name as
>provided by the user, concatenated with "Count," but I do not know how
>to make a variable name based on input.

make some temporary dictionary (tmp[dict_name]) fill it with content of file and than you
can use
exec("%sCount = tmp[%s]" % (dict_name, dict_name))

but you should check what is in dict_name (for example check if there are only
[A-z_] chars...) cause it's bit security hole

If it's possible use instead of previsious solution this one:
Count[dict_name] = tmp


cu,
maio



More information about the Python-list mailing list