2

I am reading data from a .mat file. The data is in form on numpy array.

[array([u'ABT'], dtype='<U3')] 

This is one element of the array. I want to get only the value 'ABT' from the array. Unicode normalize and Encode to ascii functions do not work.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
yyy
  • 59
  • 1
  • 6

1 Answers1

1

encode is a string method, so can't work directly on an array of strings. But there are several ways of applying it to each string

Here I'm working Py3, so the default is unicode.

In [179]: A=np.array(['one','two'])
In [180]: A
Out[180]: 
array(['one', 'two'], 
      dtype='<U3')

plain iteration:

In [181]: np.array([s.encode() for s in A])
Out[181]: 
array([b'one', b'two'], 
      dtype='|S3')

np.char has functions that apply string methods to each element of an array:

In [182]: np.char.encode(A)
Out[182]: 
array([b'one', b'two'], 
      dtype='|S3')

but it looks like this is one of the conversions that astype can handle:

In [183]: A.astype('<S3')
Out[183]: 
array([b'one', b'two'], 
      dtype='|S3')

And inspired by a recent question about np.chararray: What happened to numpy.chararray

In [191]: Ac=np.char.array(A)
In [192]: Ac
Out[192]: 
chararray(['one', 'two'], 
      dtype='<U3')
In [193]: Ac.encode()
Out[193]: 
array([b'one', b'two'], 
      dtype='|S3')
Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353