Search code examples
pythonpython-3.xencodepickle

Encode with JSON or pickle to a Variable Using Python


I know it is possible to encode a Python object to a file using

import pickle
pickle.dump(obj, file)

or you can do nearly the same using JSON, but the problem is, these all encode or decode to a file, is it possible to encode an object into a string or bytes variable instead of a file?

I am running Python 3.2 on windows.


Solution

  • Sure, just use pickle.dumps(obj) or json.dumps(obj).