472,984 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Convert Dictionary to String, vice versa?

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
6 37127
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
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
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
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
[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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rodusa | last post by:
I am looking for an example of how to save a DataTable into Base64 and vice-versa. I tried using Convert.ToBase64String(), but it says that it cannot convert DataTable to byte. Any help or pointing...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
4
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
0
by: nudrat | last post by:
Hi, My requirement are : 1) In one PC in LAN Linus is installed which is used for net surfing.... rest of the systems have xp including exchange server. 2) We want to convert all email...
5
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that...
2
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I'm pulling records from or database by dates, and there are multiple records for each part number. The part numbers are being stored as strings in a List in a custom Employee class: ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.