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

Convert Dictionary to String, vice versa?

P: n/a
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 = {"Candy" : 2.95, "Popcorn" : 4.95}
strNames = str(names)
print strNames

{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

However, is there a way to convert a string back to a dictionary?

Any help is very much appreciated!

Byron
---
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Byron wrote:
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 = {"Candy" : 2.95, "Popcorn" : 4.95}
>>> strNames = str(names)
>>> print strNames

{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

However, is there a way to convert a string back to a dictionary?

No way that I would know of. What is your intention?
Does the exact format of the string representation matter? If not, take
a look at the pickle module.
Jul 18 '05 #2

P: n/a
Byron wrote:
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 = {"Candy" : 2.95, "Popcorn" : 4.95}
>>> strNames = str(names)
>>> print strNames {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}


In this particular case, this works:
d = eval(strNames)
d

{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

But be cautious using "exec" and "eval()", as they are
potential security risks -- primarily when passing them data
that comes from a user or has been made accessible to a user,
such as by persisting it. I suspect the answer to "how?"
is in the FAQ. If not, it's definitely in the list archives.

-Peter
Jul 18 '05 #3

P: n/a
Byron wrote:
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 = {"Candy" : 2.95, "Popcorn" : 4.95}
>>> strNames = str(names)
>>> print strNames {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

However, is there a way to convert a string back to a dictionary?


use eval.
dictNames = eval(strNames)

George
Jul 18 '05 #4

P: n/a
Hi Peter,

Thank you for the information! This will come in quite handy...

Byron
---

Peter Hansen wrote:
In this particular case, this works:
>>> d = eval(strNames)
>>> d

{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

But be cautious using "exec" and "eval()", as they are
potential security risks -- primarily when passing them data
that comes from a user or has been made accessible to a user,
such as by persisting it. I suspect the answer to "how?"
is in the FAQ. If not, it's definitely in the list archives.

-Peter


Jul 18 '05 #5

P: n/a
[Byron]:

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 = {"Candy" : 2.95, "Popcorn" : 4.95}
>>> strNames = str(names)
>>> print strNames
{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

However, is there a way to convert a string back to a dictionary?

Any help is very much appreciated!


I'm surprised no one has mentioned pickle.
import pickle
names = {"Candy" : 2.95, "Popcorn" : 4.95}
pickle.dumps(names) "(dp0\nS'Popcorn'\np1\nF4.9500000000000002\nsS'Can dy'\np2\nF2.9500000000000002\ns." pickle.loads(pickle.dumps(names))

{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
--
Kjetil T.
Jul 18 '05 #6

P: n/a
Hi Kjetil,

Thank you for telling me about pickle (along with the sample of code.)
It works great... Much appreciated!

Byron
---
Kjetil Torgrim Homme wrote:
[Byron]:
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 = {"Candy" : 2.95, "Popcorn" : 4.95}
>>> strNames = str(names)
>>> print strNames
{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

However, is there a way to convert a string back to a dictionary?

Any help is very much appreciated!

I'm surprised no one has mentioned pickle.

import pickle
names = {"Candy" : 2.95, "Popcorn" : 4.95}
pickle.dumps(names)
"(dp0\nS'Popcorn'\np1\nF4.9500000000000002\nsS'Can dy'\np2\nF2.9500000000000002\ns."
pickle.loads(pickle.dumps(names))


{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.