3

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?

3
  • 1
    Any particular reason why you're doing a = print(a)? print returns None. Commented Jul 15, 2017 at 14:45
  • I added the "variable =" so that I could list the print statements in the output file statement Commented Jul 15, 2017 at 14:48
  • Ah, you're mistaken about what print does. MSeifert explains why. Commented Jul 15, 2017 at 14:48

2 Answers 2

1

print just prints the value passed in to stdout and returns None. If you want to access a property just do it without print:

import numpy as np

a = np.arange(15).reshape(3,5)
shape = a.shape
size = a.size
itemsize = a.itemsize
ndim = a.ndim
dtype = a.dtype

And if you want to print don't assign the return value of print:

print(a)
print(a.shape)
print(a.size)
print(a.itemsize)
print(a.ndim)
print(a.dtype)

Note that you don't correctly write to files, in the first case you can only write one argument at a time, you need to either str.join them or do multiple text.writes. In the second case you should check the documentation of numpy.savetxt - it expects an array as second argument not a list of several attributes.

For example:

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

# or:
#   text.write('\n'.join(map(str, [a,shape,size,itemsize,ndim,dtype])))

np.savetxt('demo_numpy.txt', a)
Sign up to request clarification or add additional context in comments.

6 Comments

When I try your suggestion I get two errors. One with each text function. TypeError: write() takes no keyword arguments and TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')
The last paragraph should explain why that happens. You're using file.write and np.savetxt incorrectly. I.e. try np.savetxt('demo_numpy.txt', a) instead.
Ok, so I want to access the values as per the first code block in your comment. But now, how do I print them out to a txt file with each variable on a new line?
Well, just text.write them one at a time and insert newlines when appropriate. :)
@MSeifert- just for clarification. I need to print the data type name, and not the datatype. Have I done this correctly? What is the difference?
|
0

I'd like to use something like this:

# import numpy as np
# my_array = np.arange(3)
metadata = [(method, getattr(my_array, method)) for method in dir(my_array) if (not callable(getattr(my_array, method))) and (not method.startswith('__'))]
names, values = zip(*metadata)  # 2 lists

Then loop over names & values and write into a file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.