By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

How to convert a String to a Dictionary?

P: 21
I have new to python programming.

I have sucessfully converted a dictionary into a string.
but i want to know how can convert the string back to the dictionary.

let us say that 'd' is dictionary

>>>from cStringIO import StringIO
>>> d={'a':'google', 'b':80, 'c':'string'}
>>> sio = StringIO()
>>> for item in d.iteritems():
... sio.write('%s, %s / ' %item)

>>> sio.getvalue()
a, google / c, string / b, 80

i have stored this value into a variable.

i want convert back this string into dictionary. how can i do this..

Plz help me out with this............
thxs in advance........
Feb 24 '09 #1
Share this Question
Share on Google+
7 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You can accomplish this by parsing the string.
Expand|Select|Wrap|Line Numbers
  1. items = [s for s in sio.getvalue().split(' / ') if s]
  2. dd = {}
  3. for item in items:
  4.     key,value = item.split(', ')
  5.     dd[key] = value
  6.  
  7. print dd
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> {'a': 'google', 'c': 'string', 'b': '80'}
  2. >>> 
Feb 24 '09 #2

P: 21
thanks for the help dude....
Feb 25 '09 #3

sharat87
P: 6
if you are saving the dictionary for later use (writing the string to a file and later reading it and reconstructing the dictionary...)

you may want to look at a serialization pattern like the pickling library.

regards,
sharat87
Feb 27 '09 #4

P: 21
thxs,..

But i'm trying to store the dictionary to database, but the database doesn't support to store the dictionary. as i had to convert the dictionary to string...
Feb 27 '09 #5

sharat87
P: 6
if that's what you want, I suggest you take at look at how you can use the pickling library... you do something like this...

import pickle as pk
d={'a':'google', 'b':80, 'c':'string'}
d_str = pk.dumps(d)
# put the string d_str into the database...
# get the string from the database and do
d = pk.loads(d_str)
# and there you have your dictionary back

i did not test the above code.. but it will be something like that..
in my opinion, it will be much easier for you to use pickling... but if you are going to use those strings for humans to read.. then forget it! :)
Feb 27 '09 #6

P: 21
no, these values are not used for the human reading..........
By using the pickling can I store the dictionary into the database......
Feb 27 '09 #7

sharat87
P: 6
yes, in the example I showed you in my previous post... you just store the d_str string into the database... that's the string you will ever need.

using dumps(d, protocol=1) will give smaller strings, but will have binary data... i am not sure if those can be written to a databse.. but the code in my above post should work just fine

Note: i did not test it.. so if u have any more problems, just drop in here :)
Feb 27 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.