Convert String to Dictionary question

Jason Orendorff jason at jorendorff.com
Thu Feb 14 16:13:05 EST 2002


> 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?

It's better to save data using pickle.
  http://www.python.org/doc/current/lib/module-pickle.html

import pickle
mydata = {'hello1': [1, 0, 0]}

# How to store data
f = open('mydata.dat', 'wb')
pickle.dump(mydata, f, 1)
f.close()

# How to load data back
f = open('mydata.dat', 'rb')
mydata = pickle.load(f)
f.close()

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list