using marshal module
Christine Davis
c.davis at actrix.co.nz
Wed Feb 2 15:05:01 EST 2000
Quinn Dunkan wrote in message ...
>On Wed, 2 Feb 2000 17:17:08 +1300, Christine Davis <c.davis at actrix.co.nz>
wrote:
[snip]
>When you opened the file, you opened it in read mode (think
>fp = fopen(filename, "r"); in C). Now, when you tried to dump your data to
>it, marshal should have thrown an IOError, but instead it silently didn't
>write anything. This seems unpython-like and a problem with marshal, to
me.
>
>What you want to do is either:
>fp = open(filename, 'r+b') # b in case it gets run on windows
>and then
>fp.seek(0)
>before dump.
>
>Or:
>fp.close()
>fp.open(filename, 'wb')
>before dump.
>
>Also, marshal.dump(datastuff['field'], fp) will store the string
>'new string of stuff' to filename, which is probably not what you want.
This is all working fine now - at least, stuff is being written back to
the file. Kind of. At the moment, it is storing a string (as an array of
characters); but the behaviour I'm looking for is to store an array of
strings. I've tried datastuff['field'][index] = 'string' but that gave me
an error (TypeError: object doesn't support item assignment)
At the moment, the file the information is stored in, has
'field': ['string', 'nextstring', 'anotherstring']
the strings have no spaces, if that helps.
Cheers,
Christine
More information about the Python-list
mailing list