0

Hello I have a problem when I tried to serialize an array with python, I have the dictionary

d={}
d['data']= [array([2, 5, 3], dtype=uint8), array([4, 1, 9], dtype=uint8)]

but my error is:

'array' is not defined

so, I have 2 questions:

1.- how can I do an array like:

array([2, 5, 3],[4, 1, 9], dtype=uint8)

to create the array I use

append()

and how can I serialize the dictionary with the form of the array(question 1) using the next function

with open('outp', 'wb') as fo:
   pickle.dump(d, fo, 2)

If I remove the words 'array' and 'dtype=uint8' it has no errors, but I think that is not the correct form to serialize an array Thanks

1
  • 1
    from numpy import array Commented Nov 8, 2016 at 2:18

2 Answers 2

0

If I remove the words 'array' and 'dtype=uint8' it has no errors

This is beacause without these words then you are simply creating a list which can be dumped by pickle indeed.

However the array you are mentionning seems like a numpy array, you can simply add from numpy import arrayin the beginning of your script.

Also, you can not make this array([2, 5, 3],[4, 1, 9]) because a 2 dimensional array with numpy is created like this : array([[2, 5, 3],[4, 1, 9]])

then once you have your array you can dump it like this:

import pickle
import numpy as np
d={}
d['data']= [np.array([2, 5, 3], dtype=np.int8), np.array([4, 1, 9], dtype=np.int8)]
with open('outp', 'wb') as fo:
    pickle.dump(d, fo, 2)
Sign up to request clarification or add additional context in comments.

Comments

0

1) According to the Numpy docs, there is a builtin method for that. Check ndarray.dump.

2) According to this entry you can wrap the dictionary in an object and save it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.