Lists and Files

Duncan Booth duncan at rcp.co.uk
Mon Mar 26 09:44:38 EST 2001


"David Andreas Alderud" <aaldv97 at student.removethispart.vxu.se> wrote in
<99njdo$oa3$1 at news.lth.se>: 

>I'm new to Python, started yesterday, is there a good way to handle
>lists and files?
>

See the documentation for the pickle and cPickle modules:
>>> stooges
['val1', 'val2', 1, [['val2_1', 'val2_2', 0], ['val2_1', 'val2_2', 0]]]
>>> fds = open('stooges', 'w')
>>> import cPickle
>>> p = cPickle.Pickler(fds)
>>> p.dump(stooges)
<Pickler object at 00827C20>
>>> fds.close()

>>> fds = open('stooges', 'r')
>>> u = cPickle.Unpickler(fds)
>>> new_stooges = u.load()
>>> print new_stooges
['val1', 'val2', 1, [['val2_1', 'val2_2', 0], ['val2_1', 'val2_2', 0]]]
>>>

Most common object types are picklable.



More information about the Python-list mailing list