Connecting Tech Pros Worldwide Forums | Help | Site Map

How to convert a String to a Dictionary?

Newbie
 
Join Date: Feb 2009
Posts: 11
#1: Feb 24 '09
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........

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#2: Feb 24 '09

re: How to convert a String to a Dictionary?


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. >>> 
Newbie
 
Join Date: Feb 2009
Posts: 11
#3: Feb 25 '09

re: How to convert a String to a Dictionary?


thanks for the help dude....
sharat87's Avatar
Newbie
 
Join Date: Feb 2009
Location: BITS-Pilani, India
Posts: 6
#4: Feb 27 '09

re: How to convert a String to a Dictionary?


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
Newbie
 
Join Date: Feb 2009
Posts: 11
#5: Feb 27 '09

re: How to convert a String to a Dictionary?


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...
sharat87's Avatar
Newbie
 
Join Date: Feb 2009
Location: BITS-Pilani, India
Posts: 6
#6: Feb 27 '09

re: How to convert a String to a Dictionary?


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! :)
Newbie
 
Join Date: Feb 2009
Posts: 11
#7: Feb 27 '09

re: How to convert a String to a Dictionary?


no, these values are not used for the human reading..........
By using the pickling can I store the dictionary into the database......
sharat87's Avatar
Newbie
 
Join Date: Feb 2009
Location: BITS-Pilani, India
Posts: 6
#8: Feb 27 '09

re: How to convert a String to a Dictionary?


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 :)
Reply

Tags
dictionary, python, string


Similar Python bytes