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']]
2
  • 1
    Have you tried pickle? Commented Dec 27, 2019 at 17:58
  • No. That seems to solve the problem. Commented Dec 27, 2019 at 18:09

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, pickled failed to save to file due to "MemoryError: ". I postulate that the data to be save was too big so I used: np.save('X',X) np.load('X.npy')
Right. 'np.save()' works better with saving large matrices and loading them back into memory.
I'm not on my computer now, feel free to update the answer. Else I'll do it later...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.