[Tutor] From SQL Blobs to Python Buffers to Actual Files

GoodPotatoes goodpotatoes at yahoo.com
Fri Jun 4 19:51:02 CEST 2010


When I write the blob/binary data to a file and give it its original file name "customer1.rtf", I am expecting the application to read the binary data into text,excel,word documents, or whatever application created the file originally.

Example:
when I write blob1 to customer1.rtf, I get "xÚµVKsÛ6î93ù{ÈÁîÈ2%Û±cŸlgœdÒ<&vLJ²\J¨" as my first line.

To get the blob in Python, I use the following:

row=cursor.execute("select top 1 * from FOO..DocUploads").fetchone()
#row[2] is the original file name, row[3] is the binary data

The blob, when selected in python is a buffer:

>>> row[3]
<read-only buffer for 0x010D7820, size 1496, offset 0 at 0x010B5EE0>

When I print row[3] I get data like "xڵVKsÛ6î93ù{ÈÁîÈ2%۱"
When I print str(row[3]) I get the same data.




________________________________
From: Steven D'Aprano <steve at pearwood.info>
To: tutor at python.org
Sent: Thu, June 3, 2010 9:18:44 PM
Subject: Re: [Tutor] From SQL Blobs to Python Buffers to Actual Files

On Fri, 4 Jun 2010 10:57:07 am GoodPotatoes wrote:

> I have been given a legacy database, and need to read the binaryfiles
> out to a disk.  The table has columns "filename" and "binaryFile",
> where the binaryFile is a BLOB
>
> My python script so far is:
>
> import pyodbc
> cnxn=pyodbc.Connection("DSN=sybasedatabase")
> cursor=cnxn.cursor()
> p=cursor.execute("select top 1 * from FOO..Table").fetchone()
> #p contains ('UsersOldFile.rtf', <read-only buffer for 0x010E1540,
> size 1496, offset 0 at 0x010C04E0>)
>
> #I tried to write this out to the disk as:
> myfile=open(p[0],'wb')
> myfile.write(p[1])
> myfile.close()
>
> #but all I get is gibberish.  Is there another way to handle the
> buffer, or something else I'm missing?

What do you mean "gibberish"? I would expect a binary blob to look 
exactly like random binary characters, in other words, gibberish, so 
what makes you think this is not working perfectly?

What do you get if you print the binary blob, and what do you expect to 
get?



-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100604/38a1434e/attachment-0001.html>


More information about the Tutor mailing list