1

I create the following:

a=np.eye(2, dtype='S17')

But when I print it I get:

print(a)
[[b'1' b'']
 [b'' b'1']]

Why does it happen and what I can do to just get the strings without b? Or should I change the way of introducing the data or the dtype?

The desired output would be:

[['1' '']
 ['' '1']]

So that I can replace this strings by others

3
  • Why not change your dtype to an int Commented Jan 24, 2014 at 18:41
  • Because I want to store strings there, therefore when I create the matrix I first say what type of data I will put inside and then add it. Commented Jan 24, 2014 at 18:42
  • 1
    stackoverflow.com/questions/6269765/… Commented Jan 24, 2014 at 18:46

1 Answer 1

2

You can use numpy.char.decode to decode the bytes literal:

In [1]: import numpy as np

In [2]: a = np.eye(2, dtype='S17')                                                                                   

In [3]: a
Out[3]: 
array([[b'1', b''],                                                                                                
       [b'', b'1']],                                                                                               
      dtype='|S17')                                                                                                

In [4]: np.char.decode(a, 'ascii')                                                                                 
Out[4]: 
array([['1', ''],                                                                                                  
       ['', '1']],                                                                                                 
      dtype='<U1')  
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of dtype='S17' changing it to '<U1' produces the same effect thanks!

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.