Marshal Obj is String or Binary?

Giovanni Bajo noway at sorry.com
Fri Jan 13 20:23:21 EST 2006


casevh at comcast.net wrote:

> Try...
>
>>>> for i in bytes: print ord(i)
>
> or
>
>>>> len(bytes)
>
> What you see isn't always what you have. Your database is capable of
> storing \ x 0 0 characters, but your string contains a single byte of
> value zero. When Python displays the string representation to you, it
> escapes the values so they can be displayed.

He can still store the repr of the string into the database, and then
reconstruct it with eval:

>>> bytes = "\x00\x01\x02"
>>> bytes
'\x00\x01\x02'
>>> len(bytes)
3
>>> ord(bytes[0])
0
>>> rb = repr(bytes)
>>> rb
"'\\x00\\x01\\x02'"
>>> len(rb)
14
>>> rb[0]
"'"
>>> rb[1]
'\\'
>>> rb[2]
'x'
>>> rb[3]
'0'
>>> rb[4]
'0'
>>> bytes2 = eval(rb)
>>> bytes == bytes2
True

-- 
Giovanni Bajo





More information about the Python-list mailing list