Reputation: 334
I'm trying to follow the example shown as the top answer here: Python insert numpy array into sqlite3 database
At first I thought it was my code, but I've tried copying and pasting the answer code exactly and I'm still getting the same error:
OSError: Failed to interpret file <_io.BytesIO object at 0x02B87C00> as a pickle
It seems there is a problem in the convert_array function that numpy isn't recognizing it as a saved file (or maybe a problem with adapt_array function that isn't storing it properly?). I am using Python 3.4.3.2 and SQLite 3.8.9. Just curious if anyone can confirm that they are getting the same error when they try to run the code in the linked answer above, and especially if anyone has insights on how to fix it and make it work.
Upvotes: 2
Views: 979
Reputation: 365817
The only problem with unutbu's code is that his adapt_array
raises an exception in Python 3:
def adapt_array(arr):
out = io.BytesIO()
np.save(out, arr)
out.seek(0)
# http://stackoverflow.com/a/3425465/190597 (R. Hill)
return buffer(out.read())
That's because buffer
doesn't exist in 3.x. And it isn't actually doing anything useful here in 2.x, so you can just remove it. Just replace that last line with:
return out.read()
And now, everything else works perfectly.
If you need compatibility with older 2.x and also with 3.x (I'm not sure if there are any versions where this overlaps, but there might be…), you can instead fix it by doing this at the top of the module:
try:
buffer
except NameError:
buffer = bytes
Upvotes: 2