Print content and several attributes of a numpy array to a text file

Viewed 2737

I have an array. My task is to print out the array, along with its shape, size, item size, dimensions, and data type name. The output should be a text file - each attribute should be on a new line.

When I attempt to use the following code, I get the error:

  File "<ipython-input-76-f4d4f45285be>", line 1, in <module>
    print(a.shape)

AttributeError: 'NoneType' object has no attribute 'shape'

I have tried two options, open a text file and np.savetxt. Neither seems to work.

Here is the code:

import numpy as np

a = np.arange(15).reshape(3,5)

a = print(a)

shape = print(a.shape)

size = print(a.size)

itemsize = print(a.itemsize)

ndim = print(a.ndim)

dtype = print(type(a.dtype))

with open("demo_numpy.tx","w") as text:
    text.write(a,shape,size,itemsize,ndim,dtype, file = text)

np.savetxt('demo_numpy.txt',[a,shape,size,itemsize,ndim,dtype])

What am I doing wrong, and how can I fix my output?

2 Answers
Related