8

I am using Spyder IDE and Python 2.7.

I have a npz file called data.npz which was given to me. I want to load this file into Spyder and view whatever is inside.

To start I've done this:

import numpy as np
data = np.load('data.npz')

In my Variable Explorer in Spyder, I have a variable with the name "data" and the type "object". When I double-click on this variable, Spyder gives an error saying "object arrays are currently not supported".

When I just type

data

I get something like this:

array({'a': array([ 1,2,3, ...,
         4,5,6]), 'b': 10, 'c': array([-1,-2,-3]), 'd': 25, 'e': 1}, dtype=object)

How do I access 'a', 'b', 'c', 'd', etc?

Community
  • 1
  • 1
Darcy
  • 619
  • 2
  • 5
  • 15
  • 1
    I've never seen anything like that, but it seems that you have an array containing a dictionary. Regular slicing like `data[0]` does not seem to work on such object. A quick solution would be to convert your array to a dict like so: `dict_data = data.tolist()` and then slice using the key values. In your case `dict_data['a']` will return `array([123,...])` and so on. – gionni May 31 '17 at 16:01
  • @gionni Awesome, worked perfect thanks. – Darcy May 31 '17 at 16:15
  • You are welcome! ;-) – gionni May 31 '17 at 16:18
  • 1
    Spyder provides Variable Explorer which is somehow very convenient in this case. Let's take the data as in the above question. If anyone works in Eclipse or IPython, `print(data.files)` results in a list `['a','b','c','d','e']`. Then, `data['a']` will return the content of `a` as @gionni presented in the above comment. – Duong Trung Nghia May 31 '17 at 19:05

3 Answers3

5

to obtain a list of all the constituent files, simply use:

$ data.files
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Sankalp Pawar
  • 51
  • 1
  • 3
-1

Try accessing each key in the dictionary as follows:

data['a']  
data['b']
shaunakde
  • 3,009
  • 3
  • 24
  • 39
  • This would be a better answer if you explained how the code you provided answers the question. – pppery Jun 17 '20 at 20:46
-1

print(data.files) will give you a list of arrays contained within the npz file. you can then use the data['a'] to view contents of a, and data['b'] to view b, so on and so forth