0

How to write, then read, (conserving all specifics) of the following list, in python?

Using various methods, I could not read the data back with the exact same formatting, datatypes, etc. I'm using python 3.6.7. Here is a toy-sample to play with

sample_list = [[np.ones(shape = (3,4), dtype='uint8'), np.int64(2), 'd654'], [np.ones(shape = (3,4), dtype='uint8'), np.int64(4), 'd654']]
Kevin Preston
  • 553
  • 1
  • 4
  • 14

1 Answers1

1

Import Pickle:

import pickle

Save Variable:

f = open('store.pckl', 'wb')
pickle.dump(sample_list, f)
f.close()

Load Variable:

f = open('store.pckl', 'rb')
obj = pickle.load(f)
f.close()

Reference: https://stackoverflow.com/a/6568495/3353760

Update: Using numpy.save()

import numpy as np

Save Variable:

np.save(file='sample_list', arr=sample_list)

Load Variable:

np.load('sample_list.npy', allow_pickle=True)
Ekaba Bisong
  • 2,918
  • 2
  • 23
  • 38