0

I keep getting this error and i dont know how to fix it i need help.

ValueError: not enough values to unpack (expected 6, got 1)

This is how i load

with open('objs.pickle', "rb") as f:
    money, hunger, thirst, energy, wanted, gun = pickle.load(f)

and this is how i save

with open('objs.pickle', 'ab') as f:  # Python 3: open(..., 'wb')
    pickle.dump([money, hunger, thirst, energy, gun, wanted], f)
Cube
  • 31
  • 2
  • 9

1 Answers1

0

pickle.load will only load the first pickled object that it finds in the file. In your case, that's a dictionary with more than two keys, so x, y = pickle.load(...) fails because it's trying to unpack the dictionary's keys to the identifiers x and y.

with open("objs.pickle") as f:
    first_dict = pickle.load(f)  # file pointer is now at end of first object
    second_dict = pickle.load(f)  # read in second object

Etc.

You are better off if you put these words into a single object, e.g. a tuple and pickle that single object. This is much easier when you don not know exactly how many pickled objects are in the file.

my_list = ['money', 'hunger', 'thirst', 'energy', 'wanted', 'gun']
tuple(my_list)

Now it is easier to pickle the tuple my_list.

prashanth manohar
  • 531
  • 1
  • 13
  • 30
  • TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' – Cube Feb 02 '17 at 02:13