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
--- 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.
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
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
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
[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.
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}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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 ---->...
|
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?
|
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,...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
| | |