Pickling arbitrary character strings on Windows

Werner Schiendl ws-news at gmx.at
Tue Mar 6 12:29:38 EST 2001


Is there a particular reason why you open the files in ASCII mode (text
mode)??
This will convert linefeeds to CRLF and use Ctrl-Z as a file delimiter (on
DOS and compatibles)

To work around that, use 'wb' instead of 'w' and 'rb' instead of 'r' as the
file mode.

This will do the work

hth
werner

Nicholas K. Sauter <nksauter at lbl.gov> wrote in message
news:3AA51D2D.3251921E at lbl.gov...
> It would be good to be able to pickle strings containing arbitrary ASCII
> characters, for example to encode data at the C level.  But on Windows
> it is impossible to do a binary unpickle of a string that has an
> embedded ASCII 26 (octal 032).  This example code tells the whole story:
>
> from pickle import dump,load
>
> def myfunc(i):
>     pro = "I_am_a_" +chr(i)+ "string"
>     f = open('try.bin','w'); dump(pro,f,1); f.close()
>     g = open('try.bin','r'); str = g.readline()# i=10 is a linefeed
> (that's OK)
>     print "read string",i,len(str),"characters"
>
> if __name__=="__main__":
>     for i in xrange(256):
>       myfunc(i)
>
> Some obvious workarounds are to use formatted output (dump(pro,f,0)) or
> to use Unix instead of Windows.  But is there any way to implement a
> practical binary string pickle on Windows?
>
> Nick Sauter, LBNL
>





More information about the Python-list mailing list