[Tutor] Structured files?

Dave Angel davea at ieee.org
Thu Jun 2 13:32:08 CEST 2011


On 01/-10/-28163 02:59 PM, Válas Péter wrote:
> 2011. június 2. 9:29 Simon Yan írta,<simonyanix at gmail.com>:
>
>> Yes you can.
>> I guess the question is how you want the information to be structured.
>> IMHO, everything in Python can be "string-lized".
>>
>> What is the syntax then?  I have Windows XP. The code is:
> f=open("xxx.dat","w")
> f.write("fff")
> d={'one':1, 2:'two'}
> f.write(d)
> f.close()
>
> Python 2.5 message:
> TypeError: argument 1 must be string or read-only character buffer, not dict
> Python 3.1 message:
> TypeError: must be str, not dict
>
> Modified code:
> f=open("xxx.dat","wb") The others remain as above, just I wrote wb.
> Python 2.5 message:
> TypeError: argument 1 must be string or read-only buffer, not dict
> Python 3.1 message:
>      f.write("fff")
> TypeError: must be bytes or buffer, not str
> This won't write even a string.
>
> I read something about a pack function, is that the key?
>

Look up shelve and pickle (both in the stdlib).  One of them will be 
suitable for the purpose.

DaveA


More information about the Tutor mailing list