473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 37196
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.