Re: Literal Escaped Octets



On Tue, 07 Feb 2006 15:06:49 +0000, Bengt Richter wrote:

On Mon, 06 Feb 2006 04:40:31 GMT, Chason Hayes <chasonh@xxxxxxxxxxx> wrote:

I am trying to convert raw binary data to data with escaped octets in
order to store it in a bytea field on postgresql server. I could do this
easily in c/c++ but I need to do it in python. I am not sure how to read
and evaluate the binary value of a byte in a long string when it is a non
printable ascii value in python. I read some ways to use unpack from the
struct module, but i really couldn't understand where that would help. I
looked at the MIMIEncode module but I don't know how to convert the object
to a string. Is there a module that will convert the data? It seems to me
that this question must have been answered a million times before but I
can't find anything.

Have you considered just encoding the data as text in hex or base64, e.g.,

>>> import binascii
>>> s = '\x00\x01\x02\x03ABCD0123'
>>> binascii.hexlify(s)
'000102034142434430313233'
>>> binascii.b2a_base64(s)
'AAECA0FCQ0QwMTIz\n'

which is also reversible later of course:
>>> h = binascii.hexlify(s)
>>> binascii.unhexlify(h)
'\x00\x01\x02\x03ABCD0123'
>>> b64 = binascii.b2a_base64(s)
>>> binascii.a2b_base64(b64)
'\x00\x01\x02\x03ABCD0123'

Regards,
Bengt Richter

I had just about come to that conclusion last night while I was working on
it. I was going to use
import base64
base64.stringencode(binarydata)
and
base64.stringdecode(stringdata)

I then wasn't sure if I should still use the bytea field or just use a
text field.

Do you have a suggestion?

.



Relevant Pages

  • Storing data into a PostgreSQL 8.1 table
    ... I'm using Delphi 2006 to insert data into a PostgreSQL 8.1 table, containing a bytea field which should store Word documents. ... Since Zeos does NOT work with D2006, I have decided to use ADO, which seems to work quite well. ... My problem is that if I interpret the bytea field as a String, and use a FileToStringroutine written by me, it stores only about the first 8k of the file! ...
    (borland.public.delphi.database.ado)
  • Storing files in PostgreSQL 8.1
    ... I'm using Delphi 2006 to insert data into a PostgreSQL 8.1 table, containing a bytea field which should store Word documents. ... I have decided to use ADO. ... My problem is that if I interpret the bytea field as a String, and use a FileToStringroutine written by me, it stores only about the first 8k of the file! ...
    (borland.public.delphi.database.ado)