473,383 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to convert a String to a Dictionary?

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
7 36003
bvdet
2,851 Expert Mod 2GB
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
neeru29
21
thanks for the help dude....
Feb 25 '09 #3
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
neeru29
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
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
neeru29
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
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

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

Similar topics

6
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names =...
7
by: manstey | last post by:
Hi, How do I convert a string like: a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}" into a dictionary: b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS':...
7
by: Wilson | last post by:
Hi, How do get the Dictioanry object from FiedlInfo ? my code : fieldInfo = this.GetType().GetField("dictioanry1"); ??Dictionary<string, string> dicTemp1 = (Dictionary<string,...
4
by: James Page | last post by:
Hi all I have a shopping cart object which I'd like to send the contents via an e-mail. I get an error saying 'hybridDictionary' cannot be converted to string. Does anyone know how to do...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
15
GTXY20
by: GTXY20 | last post by:
Hi, I have the following in a text file: 1,a 1,b 1,b 2,a 2,c 2,a
3
by: Ameet Nanda | last post by:
Hi All, I access net using a proxy, which I have to authenticate everytime I try to access net from my system. Now when I use urllib2.urlopen(url) , I cant get ahead. I must provide proxy...
35
by: Abandoned | last post by:
I want to convert a string to command.. For example i have a string: a="" I want to do this list.. How can i do ?
1
by: alex21 | last post by:
Ok i am trying to use a Linq query to access a dictionary. public static Dictionary<string, Client> Clients = new Dictionary<string, Client>();Using this Linq query: IEnumerable<Staff> loginquery...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.