Marshal Obj is String or Binary?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Jan 13 17:52:32 EST 2006


In <1137183910.659813.210550 at g47g2000cwa.googlegroups.com>, Mike wrote:

> The example below shows that result of a marshaled data structure is
> nothing but a string
> 
>>>> data = {2:'two', 3:'three'}
>>>> import marshal
>>>> bytes = marshal.dumps(data)
>>>> type(bytes)
> <type 'str'>
>>>> bytes
> '{i\x02\x00\x00\x00t\x03\x00\x00\x00twoi\x03\x00\x00\x00t\x05\x00\x00\x00three0'
> 
> Now, I need to store this data safely in my database as CLEAR TEXT, not
> BLOB. It seems to me that it should work just fine since it is string
> anyways. So, why does O'reilly's Python Cookbook is insisting in saving
> it as a binary file and BLOB type?
> 
> Am I missing out something?

Yes, that a string is *binary* data.  But only a subset of strings is safe
to use as `TEXT` in databases.  Do you see all those '\x??' escapes? 
'\x00' is *one* byte!  A byte with the value zero.  Something your DB
doesn't allow in a `TEXT` type.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list