How to convert a String to a Dictionary? | Newbie | | Join Date: Feb 2009
Posts: 11
| | |
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........
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,566
| | | re: How to convert a String to a Dictionary?
You can accomplish this by parsing the string. - items = [s for s in sio.getvalue().split(' / ') if s]
-
dd = {}
-
for item in items:
-
key,value = item.split(', ')
-
dd[key] = value
-
-
print dd
Output: - >>> {'a': 'google', 'c': 'string', 'b': '80'}
-
>>>
| | Newbie | | Join Date: Feb 2009
Posts: 11
| | | re: How to convert a String to a Dictionary?
thanks for the help dude....
|  | Newbie | | Join Date: Feb 2009 Location: BITS-Pilani, India
Posts: 6
| | | 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
| | | 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...
|  | Newbie | | Join Date: Feb 2009 Location: BITS-Pilani, India
Posts: 6
| | | 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
| | | 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......
|  | Newbie | | Join Date: Feb 2009 Location: BITS-Pilani, India
Posts: 6
| | | 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 :)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|