Adonis Vargas
2/7/2008 4:43:00 AM
Amit Gupta wrote:
> Need a python trick, if it exists:
>
> I have a file that stores key, value in following format
> --
> "v1" : "k1",
> "v2" : "k2"
> --
>
> Is there a way to directly load this file as dictionary in python. I
> could do (foreach line in file, split by ":" and then do dictionary
> insert). Wondering, if some python built-in function can just read a
> valid dictionary-file and load it?
>
> Thanks
If you could change the input data you can pickle the dict object then
save and reopen as needed.
import cPickle
data = {'k1': 'v1', 'k2': 'v2'}
output = open('output.dat', 'wb')
cPickle.dump(data, output)
output.close()
data1 = cPickle.load(open('output.dat', 'rb'))
print type(data1)
print data1
Hope this helps.
Adonis Vargas