Vacassal Alsk Vacassal Alsk - 1 month ago 4
Python Question

how does pickle know which to pick?

I have my pickle function working properly

with open(self._prepared_data_location_scalar, 'wb') as output:
# company1 = Company('banana', 40)
pickle.dump(X_scaler, output, pickle.HIGHEST_PROTOCOL)
pickle.dump(Y_scaler, output, pickle.HIGHEST_PROTOCOL)


with open(self._prepared_data_location_scalar, 'rb') as input_f:
X_scaler = pickle.load(input_f)
Y_scaler = pickle.load(input_f)


However, I am very curious how does pickle know which to load? Does it mean that everything has to be in the same sequence?

Answer

wow I did not even know you could do this ... and I have been using python for a very long time... so thats totally awesome in my book, however you really should not do this it will be very hard to work with later(especially if it isnt you working on it)

I would recommend just doing

pickle.dump({"X":X_scalar,"Y":Y_scalar},output)

 ...
data = pickle.load(fp)
print "Y_scalar:",data['Y']
print "X_scalar:",data['X']

unless you have a very compelling reason to save and load the data like you were in your question ...

edit to answer the actual question...

it loads from the start of the file to the end (ie it loads them in the same order they were dumped)

Comments