Convert String to Dictionary question

Christian Tanzer tanzer at swing.co.at
Fri Feb 15 09:27:49 EST 2002


quinn at hurl.ugcs.caltech.edu (Quinn Dunkan) wrote:

> On Thu, 14 Feb 2002 21:27:48 +0000, John J. Lee <jjl at pobox.com> wrote:
> >On Thu, 14 Feb 2002, MDK wrote:
> >
> >> I saved my dictionary {'hello1': [1, 0, 0]} to a file.
> >>
> >> When I do a readline the I get back a string.
> >>
> >> How do I convert this string to a dictionary?
> >
> >You probably don't want to.  Read the documentation for the pickle module.
>
> Or if you do, say because 'repr' format is more human readable than
> pickle, do 'd = eval(fp.readline())'. As always with eval(), if bad
> guys can edit your file, you're in trouble. Of course, if they can
> edit your pickle, you're in trouble too.

Not quite:

>>> import os
>>> os.system ("echo 'kilroy'")
kilroy
0
>>> eval ("""os.system ("echo 'kilroy'")""")
kilroy
0
>>> eval ("""os.system ("echo 'kilroy'")""", {}, {})
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 0, in ?
NameError: name 'os' is not defined

Making pickle safe is left as an exercise ;-)

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list