storing pickles in sql data base

Nick Craig-Wood nick at craig-wood.com
Wed Jul 11 22:30:04 CEST 2007


David Bear <david.bear at asu.edu> wrote:
>  I need to store pickled objects in postgresql. I reading through the pickle
>  docs it says to always open a file in binary mode because you can't be sure
>  if the pickled data is binary or text. So I have 2 question. Can I set the
>  pickle to be text -- and then store it in a 'text' type field in my sql
>  table, or should what sql type should I save the pickle as?

You could always encode it into text form, eg

  >>> from cPickle import dumps, loads
  >>> a = range(10)
  >>> a
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  >>> b = dumps(a).encode("zip").encode("base64").strip()
  >>> b
  'eJzTyCkw5PI04Er0NARiIyA2BmITIDYFYjMgNgdiCyC25ErUAwD5DQqD'
  >>> loads(b.decode("base64").decode("zip"))
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  >>> 

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list