Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Convert Dictionary to String, vice versa?

Reply
Thread Tools

Convert Dictionary to String, vice versa?

 
 
Byron
Guest
Posts: n/a
 
      09-15-2004
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
---
 
Reply With Quote
 
 
 
 
Benjamin Niemann
Guest
Posts: n/a
 
      09-15-2004
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.
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      09-15-2004
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
 
Reply With Quote
 
George Yoshida
Guest
Posts: n/a
 
      09-15-2004
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
 
Reply With Quote
 
Byron
Guest
Posts: n/a
 
      09-15-2004
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


 
Reply With Quote
 
Kjetil Torgrim Homme
Guest
Posts: n/a
 
      09-15-2004
[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.
 
Reply With Quote
 
Byron
Guest
Posts: n/a
 
      09-15-2004
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}
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert std::string to std::vector<unsigned char> and vice versa timor.super@gmail.com C++ 3 04-02-2007 02:12 PM
how to convert narrow string to wide string and vice versa? thinktwice C++ 4 09-07-2006 10:55 AM
How to convert '1' into '-1' and vice versa? - Java 8 06-11-2005 09:28 AM
Why convert Word doc to PDF? Or vice versa Harry Computer Support 3 03-08-2005 12:44 PM
Convert a string to float and vice versa Flyingaway C++ 6 02-19-2005 11:08 PM



Advertisments