Convert Dictionary to String, vice versa?

B

Byron

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
---
 
B

Benjamin Niemann

Byron said:
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:

{'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.
 
P

Peter Hansen

Byron said:
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:

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

In this particular case, this works:
{'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
 
G

George Yoshida

Byron said:
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:

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

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

use eval.

George
 
K

Kjetil Torgrim Homme

[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.
{'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
 
B

Byron

Hi Kjetil,

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

Byron
---

[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.

"(dp0\nS'Popcorn'\np1\nF4.9500000000000002\nsS'Candy'\np2\nF2.9500000000000002\ns."

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top