Write and read objects from files .
Chris Barker
chrishbarker at home.net
Mon Oct 29 13:58:01 EST 2001
> > I'm a python beginner and looking for :
> > 1. A way to save a list on a file (as a string) and then retrieve it
> > back (read the string from the file and build automatically a list) .
you might want to try repr() and eval(). In general, the goal of repr()
is to produce a string such that:
eval(repr(x)) = x
so you can do:
file.write(repr(YourList))
and later:
eval(file.read())
You'r reading code will probably be a lot more complex, if you have more
than one item stored in the file, but this should give you the idea.
Here is a short example at the command line:
>>> l1 = ['this', 'that', '56']
>>> l1
['this', 'that', '56']
>>> s = repr(l1)
>>> s
"['this', 'that', '56']"
>>> l2 = eval(s)
>>> l2
['this', 'that', '56']
>>> l2 == l1
1
>>>
-Chris
--
Christopher Barker,
Ph.D.
ChrisHBarker at home.net --- --- ---
http://members.home.net/barkerlohmann ---@@ -----@@ -----@@
------@@@ ------@@@ ------@@@
Oil Spill Modeling ------ @ ------ @ ------ @
Water Resources Engineering ------- --------- --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------
More information about the Python-list
mailing list