I have a process that pickles a dictionary using Python 3.2. I then need to unpickle this dictionary using Python 2.7 or 2.6. The problem is that when transferring between python versions I get a dictionary full of unicode data which upsets the Python API I am trying to feed it into.
Pickling in Python 3.2:
myDict = {'a': 'first', 'b': 'second', 'c': 'third'}
with open(file, 'wb') as f:
pickle.dump(myDict, f, 2)
Unpickling in Python 2.6:
with open(file, f) as f:
myDict = pickle.load(f)
returns: {u’a’: u’first’, u’c’: u’third’, u’b’: u’second’}
How can I get back exactly what I put in (i.e. not unicode)?
You’re actually getting back exactly what you put in, since strings in Python 3 are unicode
To get
str‘s, you can convert the keys and values in the dictionary: