Re: newbie question: how to read back the dictionary from a file?

2007-04-16 Thread Alex Martelli
lancered <[EMAIL PROTECTED]> wrote:

> Hi Dear all,
> 
> I have some data here in the form of a dictionary, called "vdic". Then
> I write them to a data file "f" using   the write function as
> f.write(str(vdic)).  The keys of this dictionary are integers and
> values are float numbers. Something like this:
> 
> { 1: 0.00951486513347, 2: 0.0388123556019, ... ...}
> 
> Now, I want to read these data back in another function.  Of course, I
> could parse the string little by little, e.g, first read a "{", then
> loop read a int, then read a ":", then a float etc etc... Since it is
> written out with standard python builtin functions,  I guess there may
> be some more direct method than this, say a function in some modules?
> Could someone give me a hint?

Others have suggested better ways of writing the file out.  However,
it's possible to recover the data from the string form you've written,
via the eval builtin function -- with all sorts of caveats (won't be as
fast as other approaches, enormous security risk if anybody could have
tampered with the file, etc).

To mitigate the security risks a little, try

eval(thestr, dict(__builtins__={}))

or more advanced approaches such as discussed at
,

etc.


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


Re: newbie question: how to read back the dictionary from a file?

2007-04-16 Thread Fuzzyman
On Apr 16, 11:03 am, "lancered" <[EMAIL PROTECTED]> wrote:
> Hi Dear all,
>
> I have some data here in the form of a dictionary, called "vdic". Then
> I write them to a data file "f" using   the write function as
> f.write(str(vdic)).  The keys of this dictionary are integers and
> values are float numbers. Something like this:
>
> { 1: 0.00951486513347, 2: 0.0388123556019, ... ...}
>
> Now, I want to read these data back in another function.  Of course, I
> could parse the string little by little, e.g, first read a "{", then
> loop read a int, then read a ":", then a float etc etc... Since it is
> written out with standard python builtin functions,  I guess there may
> be some more direct method than this, say a function in some modules?
> Could someone give me a hint?


ConfigObj and its 'unrepr' mode gives you a useful (and simple) way of
preserving and restoring basic Python datatypes.

The file format is a very readable 'ini' format - and the basic
interface is like a dictionary, for both writing and retrieving
values.

http://www.voidspace.org.uk/python/configobj.html

Fuzzyman

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


Re: newbie question: how to read back the dictionary from a file?

2007-04-16 Thread 7stud
s.close()

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


Re: newbie question: how to read back the dictionary from a file?

2007-04-16 Thread 7stud
On Apr 16, 4:03 am, "lancered" <[EMAIL PROTECTED]> wrote:
> Hi Dear all,
>
> I have some data here in the form of a dictionary, called "vdic". Then
> I write them to a data file "f" using   the write function as
> f.write(str(vdic)).  The keys of this dictionary are integers and
> values are float numbers. Something like this:
>
> { 1: 0.00951486513347, 2: 0.0388123556019, ... ...}
>
> Now, I want to read these data back in another function.  Of course, I
> could parse the string little by little, e.g, first read a "{", then
> loop read a int, then read a ":", then a float etc etc... Since it is
> written out with standard python builtin functions,  I guess there may
> be some more direct method than this, say a function in some modules?
> Could someone give me a hint?

Try:

import shelve

s = shelve.open("newFile.dat")
s["d"] = {"red":2, 3:"blue", 2.5:"x"}
s.close()

s = shelve.open("newFile.dat")
print s["d"]
my_dict = s["d"]
print my_dict[2.5]

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


Re: newbie question: how to read back the dictionary from a file?

2007-04-16 Thread Amit Khemka
On 16 Apr 2007 03:03:40 -0700, lancered <[EMAIL PROTECTED]> wrote:
> Hi Dear all,
>
> I have some data here in the form of a dictionary, called "vdic". Then
> I write them to a data file "f" using   the write function as
> f.write(str(vdic)).  The keys of this dictionary are integers and
> values are float numbers. Something like this:
>
> { 1: 0.00951486513347, 2: 0.0388123556019, ... ...}
>
> Now, I want to read these data back in another function.  Of course, I
> could parse the string little by little, e.g, first read a "{", then
> loop read a int, then read a ":", then a float etc etc... Since it is
> written out with standard python builtin functions,  I guess there may
> be some more direct method than this, say a function in some modules?
> Could someone give me a hint?
>

Check out cPickle or shelve modules.

Cheers,
-- 

Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
-- 
http://mail.python.org/mailman/listinfo/python-list


newbie question: how to read back the dictionary from a file?

2007-04-16 Thread lancered
Hi Dear all,

I have some data here in the form of a dictionary, called "vdic". Then
I write them to a data file "f" using   the write function as
f.write(str(vdic)).  The keys of this dictionary are integers and
values are float numbers. Something like this:

{ 1: 0.00951486513347, 2: 0.0388123556019, ... ...}

Now, I want to read these data back in another function.  Of course, I
could parse the string little by little, e.g, first read a "{", then
loop read a int, then read a ":", then a float etc etc... Since it is
written out with standard python builtin functions,  I guess there may
be some more direct method than this, say a function in some modules?
Could someone give me a hint?

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