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

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
llrs
  • 3,308
  • 35
  • 68

1 Answers1

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')  
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504