1

In python 2.7.6:

# the data i'm trying to pickle
>>> x[0:5]
[494.12804680901604, 641.9374923706055, 778.293918918919, 470.2265625, 237.21332017010934]
>>> y[0:5]
[236.99996948242188, 381.6793310733242, 685.0, 409.0909090909091, 658.0]
>>> z[0:5]
[23, 20, 98, 24, 78]
>>> holder = [x,y,z]

How i'm pickling:

with open('holderData.obj','wb') as f:
    pickle.dump(holder,f)
f.close()

In python 3.6.2

with open('holderData.obj','rb') as f:
     d = pickle.load(f, encoding='bytes') 

Yet, this returns:

Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: could not convert string to float

The only question/answer I could found related to this issue, tells me to add the encoding='bytes' bit which doesn't work in this instance.

The pickle itself print(repr(pickle.dumps(holder))):

'(lp0\n(lp1\nF494.12804680901604\naF641.9374923706055\naF778.293918918919\naF470.2265625\naF237.21332017010934\naF372.76081123737373\naF396.15337968952133\naF615.2265625\naF470.2265625\naF581.2155330882352\naF488.40675200803213\naF475.47189597315435\naF92.0511279585
Mitchell van Zuylen
  • 3,905
  • 4
  • 27
  • 64
  • See the **Data stream formats (protocols)** section of [this answer](https://stackoverflow.com/questions/4529815/saving-an-object-data-persistence/4529901#4529901) to another question. – martineau Aug 06 '17 at 19:47
  • Can you show the pickle itself (i.e., the result of `print(repr(pickle.dumps(holder)))`? And what are the Python types of `x`, `y` and `z`? (They *look* like lists, but it's not 100% clear.) – Mark Dickinson Aug 06 '17 at 19:49
  • @MarkDickinson Added the pickle itself. `x`,`y`,and `z` are all lists, each a little under 3000 in length. – Mitchell van Zuylen Aug 06 '17 at 19:56
  • 1
    @martineau Setting it to highest priority did the trick. – Mitchell van Zuylen Aug 06 '17 at 20:04
  • 1
    Actually I can't reproduce this at all. Are you sure there's nothing weird in the lists after index 4? – MSeifert Aug 06 '17 at 20:16
  • Mitchell: It might be better to find a literal numeric value that works in both versions instead of `HIGHEST_PROTOCOL` (or `-1`). Chose the largest number that works in both. Being explicit will protect you from it changing if (when) you upgrade either one to later version (and it breaking your code). – martineau Aug 06 '17 at 22:21
  • @martineau: The protocol shouldn't matter, though: Python 3 can read any pickle protocol that Python 2 can write. – Mark Dickinson Aug 07 '17 at 06:05
  • @Mark: True. I was mistakenly thinking of bi-directional compatibility, not just Py2 → Py3 (the subject of this question). – martineau Aug 07 '17 at 13:20

0 Answers0