Convert String to Dictionary question 
Author Message
 Convert String to Dictionary question

I saved my dictionary {'hello1': [1, 0, 0]} to a file.

When I do a readline the I get back a string.

How do I convert this string to a dictionary?

Thanks.



Tue, 03 Aug 2004 04:53:52 GMT  
 Convert String to Dictionary question

Quote:

> I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> When I do a readline the I get back a string.

> How do I convert this string to a dictionary?

> Thanks.

You eval() it?

Quote:
>>> d = { "one":1, "two":2, "three":3 }
>>> print d

{'three': 3, 'two': 2, 'one': 1}
Quote:
>>> S = str(d)
>>> print S

{'three': 3, 'two': 2, 'one': 1}
Quote:
>>> dd = eval(S)
>>> print dd

{'one': 1, 'three': 3, 'two': 2}

Quote:
>>> d is dd
0

Do I pass??

:-)

-gustavo



Tue, 03 Aug 2004 04:49:41 GMT  
 Convert String to Dictionary question

Quote:

> I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> When I do a readline the I get back a string.

> How do I convert this string to a dictionary?

You probably don't want to.  Read the documentation for the pickle module.

John



Tue, 03 Aug 2004 05:27:48 GMT  
 Convert String to Dictionary question

Quote:

> > I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> > When I do a readline the I get back a string.

> > How do I convert this string to a dictionary?

> > Thanks.

> You eval() it?

> >>> d = { "one":1, "two":2, "three":3 }
> >>> print d
> {'three': 3, 'two': 2, 'one': 1}
> >>> S = str(d)
> >>> print S
> {'three': 3, 'two': 2, 'one': 1}
> >>> dd = eval(S)
> >>> print dd
> {'one': 1, 'three': 3, 'two': 2}
> >>> d is dd
> 0

> Do I pass??

> :-)

> -gustavo

I never would have figured that one out.  Even after reading the
documentation it still does not appear that the eval() function would do
that.

Thanks for the info.



Tue, 03 Aug 2004 05:19:58 GMT  
 Convert String to Dictionary question



Quote:

> > I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> > When I do a readline the I get back a string.

> > How do I convert this string to a dictionary?

> You probably don't want to.  Read the documentation for the pickle module.

> John

Pickling seems too slow.


Tue, 03 Aug 2004 05:32:04 GMT  
 Convert String to Dictionary question

Quote:
> I saved my dictionary {'hello1': [1, 0, 0]} to a file.
> When I do a readline the I get back a string.
> How do I convert this string to a dictionary?

It's better to save data using pickle.
  http://www.python.org/doc/current/lib/module-pickle.html

import pickle
mydata = {'hello1': [1, 0, 0]}

# How to store data
f = open('mydata.dat', 'wb')
pickle.dump(mydata, f, 1)
f.close()

# How to load data back
f = open('mydata.dat', 'rb')
mydata = pickle.load(f)
f.close()

## Jason Orendorff    http://www.jorendorff.com/



Tue, 03 Aug 2004 05:13:05 GMT  
 Convert String to Dictionary question
even better to use to use cPickle.


Quote:
> > I saved my dictionary {'hello1': [1, 0, 0]} to a
> file.
> > When I do a readline the I get back a string.
> > How do I convert this string to a dictionary?

> It's better to save data using pickle.

http://www.python.org/doc/current/lib/module-pickle.html

Quote:

> import pickle
> mydata = {'hello1': [1, 0, 0]}

> # How to store data
> f = open('mydata.dat', 'wb')
> pickle.dump(mydata, f, 1)
> f.close()

> # How to load data back
> f = open('mydata.dat', 'rb')
> mydata = pickle.load(f)
> f.close()

> ## Jason Orendorff    http://www.jorendorff.com/

> --
> http://mail.python.org/mailman/listinfo/python-list

__________________________________________________
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com


Tue, 03 Aug 2004 05:29:56 GMT  
 Convert String to Dictionary question

Quote:

> > I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> > When I do a readline the I get back a string.

> > How do I convert this string to a dictionary?

> > Thanks.

> You eval() it?

> >>> d = { "one":1, "two":2, "three":3 }
> >>> print d
> {'three': 3, 'two': 2, 'one': 1}
> >>> S = str(d)
> >>> print S
> {'three': 3, 'two': 2, 'one': 1}
> >>> dd = eval(S)
> >>> print dd
> {'one': 1, 'three': 3, 'two': 2}
> >>> d is dd
> 0

This works, but is generally a Bad Idea(tm).  The problem
with eval/exec on arbitrary strings (and any string you read
in from a file is arbitrary) is that it's hard to be
positive that what you're eval/exec-ing is what you expect.
And if it's *not*, then many, many very bad things can
happen.  (Imagine someone "accidentally" replacing that
textfile with one that contains the line "import
os;os.system('rm -s /')" -- suddenly your entire filesystem
is blank....)

If you want to store the contents of a dictionary on a
permanent disk file, you should look at the Pickle and
Shelve modules.  If you're doing this a lot, or with lots of
data, then try using one of the generic db modules (anydbm,
bsddb, etc).

Jeff Shannon
Technician/Programmer
Credit International



Tue, 03 Aug 2004 06:19:49 GMT  
 Convert String to Dictionary question

Quote:

>> I saved my dictionary {'hello1': [1, 0, 0]} to a file.

>> When I do a readline the I get back a string.

>> How do I convert this string to a dictionary?

>You probably don't want to.  Read the documentation for the pickle module.

Or if you do, say because 'repr' format is more human readable than pickle,
do 'd = eval(fp.readline())'.  As always with eval(), if bad guys can edit your
file, you're in trouble.  Of course, if they can edit your pickle, you're in
trouble too.


Tue, 03 Aug 2004 06:23:12 GMT  
 Convert String to Dictionary question

Quote:

> Pickling seems too slow.

Really?  What makes you say this?

In some random tests on my box, it seems that
repr() is a {*filter*}sy bit faster than cPickle.dumps(), and
eval() is several times slower than cPickle.loads().

## Jason Orendorff     http://www.*-*-*.com/



Tue, 03 Aug 2004 07:17:49 GMT  
 Convert String to Dictionary question

Quote:




> > > I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> > > When I do a readline the I get back a string.

> > > How do I convert this string to a dictionary?

> > You probably don't want to.  Read the documentation for the pickle module.

> Pickling seems too slow.

Are you using Pickle or cPickle?  They provide the same functionality, but
cPickle is implemented in C and is thus notably faster.  Pickle is kept around
because it's possible to subclass from it, whereas it's not possible to subclass
C code (though for most purposes, subclassing a pickler is unnecessary).

I suppose this may change soon--as of 2.2, types should be subclassable, so
there shouldn't be any further need to maintain the slower all-Python version.

Jeff Shannon
Technician/Programmer
Credit International



Tue, 03 Aug 2004 08:20:33 GMT  
 Convert String to Dictionary question

Quote:


> > > I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> > > When I do a readline the I get back a string.

> > > How do I convert this string to a dictionary?

> > > Thanks.

> > You eval() it?

> > >>> d = { "one":1, "two":2, "three":3 }
> > >>> print d
> > {'three': 3, 'two': 2, 'one': 1}
> > >>> S = str(d)
> > >>> print S
> > {'three': 3, 'two': 2, 'one': 1}
> > >>> dd = eval(S)
> > >>> print dd
> > {'one': 1, 'three': 3, 'two': 2}
> > >>> d is dd
> > 0

> This works, but is generally a Bad Idea(tm).  The problem
> with eval/exec on arbitrary strings (and any string you read
> in from a file is arbitrary) is that it's hard to be
> positive that what you're eval/exec-ing is what you expect.
> And if it's *not*, then many, many very bad things can
> happen.  (Imagine someone "accidentally" replacing that
> textfile with one that contains the line "import
> os;os.system('rm -s /')" -- suddenly your entire filesystem
> is blank....)

Writing python scripts works but is generally a Bad Idea(tm).

Imagine somone "accidentally" replacing the textfile containing your
script with one that contains the line "import os;os.system('rm -s
/')" -- suddenly your entire filesystem is blank....)

Sic probo.

:-)



Tue, 03 Aug 2004 16:19:05 GMT  
 Convert String to Dictionary question
Okay, I am conviced that I should use cPickle over evel().

However, this brings me to another question.  Athough I use Python 2.2 I
need to be sure that everything I do is compatable with 1.5.  Is there a
chart somewhere that will show me, for example, that cPickle will work with
both versions?

Thanks for all the replies.



Tue, 03 Aug 2004 21:13:10 GMT  
 Convert String to Dictionary question

Quote:

> > Pickling seems too slow.

> Really?  What makes you say this?

> In some random tests on my box, it seems that
> repr() is a {*filter*}sy bit faster than cPickle.dumps(), and
> eval() is several times slower than cPickle.loads().

> ## Jason Orendorff     http://www.*-*-*.com/

Here is a copy/paste from the documentation (see note at end):

#-----------------------------------------------------------------

3.14 pickle -- Python object serialization

The pickle module implements a basic but powerful algorithm for ``pickling''
(a.k.a. serializing, marshalling or flattening) nearly arbitrary Python
objects. This is the act of converting objects to a stream of bytes (and
back: ``unpickling''). This is a more primitive notion than persistence --
although pickle reads and writes file objects, it does not handle the issue
of naming persistent objects, nor the (even more complicated) area of
concurrent access to persistent objects. The pickle module can transform a
complex object into a byte stream and it can transform the byte stream into
an object with the same internal structure. The most obvious thing to do
with these byte streams is to write them onto a file, but it is also
conceivable to send them across a network or store them in a database. The
module shelve provides a simple interface to pickle and unpickle objects on
DBM-style database files.

Note: The pickle module is rather slow. A reimplementation of the same
algorithm in C, which is up to 1000 times faster, is available as the
cPickle module. This has the same interface except that Pickler and
Unpickler are factory functions, not classes (so they cannot be used as base
classes for inheritance).

#-----------------------------------------------------------------

My concern was that even though cPickle is supposed to be faster is does not
mean that is it as fast as it needs to be.



Tue, 03 Aug 2004 21:40:34 GMT  
 Convert String to Dictionary question

Quote:



> >> I saved my dictionary {'hello1': [1, 0, 0]} to a file.

> >> When I do a readline the I get back a string.

> >> How do I convert this string to a dictionary?

> >You probably don't want to.  Read the documentation for the pickle module.

> Or if you do, say because 'repr' format is more human readable than
> pickle, do 'd = eval(fp.readline())'. As always with eval(), if bad
> guys can edit your file, you're in trouble. Of course, if they can
> edit your pickle, you're in trouble too.

Not quite:

Quote:
>>> import os
>>> os.system ("echo 'kilroy'")

kilroy
0
Quote:
>>> eval ("""os.system ("echo 'kilroy'")""")

kilroy
0
Quote:
>>> eval ("""os.system ("echo 'kilroy'")""", {}, {})

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 0, in ?
NameError: name 'os' is not defined

Making pickle safe is left as an exercise ;-)

--

Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92



Tue, 03 Aug 2004 22:27:49 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. convert dictionary to string

2. Questions on converting/updating dictionary files in C4

3. Dictionary to string and back to dictionary??

4. How to convert a string into a class (Beginner's question)

5. Question on converting string to symbol

6. newbie question: converting integer to string

7. How to convert binary string to char string ?

8. CIN: Converting a C String to a LabVIEW String

9. Using scan from string to convert a string into a number

10. how convert decimal string to hexa string

11. Converting a Hexadecimal String to a Binary String

12. String#split converts string args to regexes -- ?

 

 
Powered by phpBB® Forum Software