python - Conversion from U3 dtype to ascii -
i reading data .mat file. data in form on numpy array.
[array([u'abt'], dtype='<u3')]
this 1 element of array. want value 'abt' array. unicode normalize , encode ascii functions not work.
encode
string method, can't work directly on array of strings. there several ways of applying each string
here i'm working py3, default unicode.
in [179]: a=np.array(['one','two']) in [180]: out[180]: array(['one', 'two'], dtype='<u3')
plain iteration:
in [181]: np.array([s.encode() s in a]) out[181]: array([b'one', b'two'], dtype='|s3')
np.char
has functions apply string methods each element of array:
in [182]: np.char.encode(a) out[182]: array([b'one', b'two'], dtype='|s3')
but looks 1 of conversions astype
can handle:
in [183]: a.astype('<s3') out[183]: array([b'one', b'two'], dtype='|s3')
and inspired recent question np.chararray
: what happened 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')
Comments
Post a Comment