Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:09:06+00:00 2026-06-15T10:09:06+00:00

I am using cPickle and psycopg2 to store some vectors into database. This is

  • 0

I am using cPickle and psycopg2 to store some vectors into database.
This is my code to store binary data

binary_vec = cPickle.dumps(vec, -1)
db.cur.execute('''
               INSERT INTO feature_vector (vector, id) 
               VALUES (%s, %s);
               ''', (psycopg2.Binary(binary_vec), thread_id)
db.conn.commit()

However when I use fetchall() to load my data back, the type is buffer. I can’t find how how to restore this buffer object back to a list (vec).

This is how I fetch the data

db.cur.execute("SELECT * FROM feature_vector;")
m = db.cur.fetchall()

The result looks like this

[(3169187, <read-only buffer for 0x1002b0f10, size 3462, offset 0 at 0x1004a7430>), 
(3169275, <read-only buffer for 0x1002b0f50, size 3462, offset 0 at 0x1004a7570>), 
(3169406, <read-only buffer for 0x1002b0f70, size 3462, offset 0 at 0x10140b0b0>), 
(3169541, <read-only buffer for 0x10141c030, size 3462, offset 0 at 0x10140b2b0>), 
(3169622, <read-only buffer for 0x10141c050, size 3462, offset 0 at 0x10140b3f0>),...

When I try to use cPickle.loads(m[0][1]), it will return the error message

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string, not buffer
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:09:07+00:00Added an answer on June 15, 2026 at 10:09 am

    You can create a customized typecaster to automatically convert pickled values to Python:

    import cPickle
    obj = {'a': 10}
    data = cPickle.dumps(obj, -1)
    
    import psycopg2
    
    def cast_pickle(data, cur):
        if data is None: return None
        return cPickle.loads(str(psycopg2.BINARY(data, cur)))
    
    psycopg2.extensions.register_type(
        psycopg2.extensions.new_type(
            psycopg2.BINARY.values, 'BINARY-PICKLE', cast_pickle))
    
    cnn = psycopg2.connect('')
    cur = cnn.cursor()
    cur.execute("select %s::bytea", [psycopg2.Binary(data)])
    cur.fetchone()
    # ({'a': 10},)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python 2.5 I stored data using this code: def GLWriter(file_name, string): import cPickle
To store a complex object to Datastore i'm currently using this approach: class PickleProperty(db.Property):
Using this code, the following execution yields strange results: C 100 R W The
I'm using this simple code and observing monotonically increasing memory usage. I'm using this
Using string functions and a whole lot of if-else turns this into a real
Using MongoDB C# Driver (official form 10gen), I'm doing the following code: using (database.RequestStart())
Using myObject.performClick() I can simulate click event from the code. Does something like this
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
Python - Using cPickle to load a previously saved pickle uses too much memory?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.