Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the string value from numpy array?

When I store string data as characters in a numpy array, and retrieve the values later, it always returns a value as b'x' for 'x' I stored earlier. Currently I am using the stupid way to extract the value by doing str(some_array[row, col...]).lstrip("b'").rstrip("'"), I believe there should be an easier way to do this. Does anyone know? Thanks!

like image 852
user3833107 Avatar asked Nov 09 '22 11:11

user3833107


1 Answers

Based on your output you're using Python 3 (where bytes and str are different types). This answer applies to arrays with dtype='S' (a bytestring); for dtype=str or dtype='U' they are stored as unicode strings (at least for python 3) and there is no issue.

The easiest thing to do is probably

str(some_array[row,col...],encoding='ascii')

Note that you can use other encodings instead of ascii ('UTF-8' is common) and which one is right depends on which one you used to put your data into the numpy array. If you're using non-exotic alphanumeric characters it shouldn't really matter though. If you've put an str into the array, numpy uses ascii to encode it, so unless you've gone to special effort to do something different, 'ascii' should be right.

(For reference, I found this answer helpful https://stackoverflow.com/a/14010552/4657412)

like image 79
DavidW Avatar answered Nov 14 '22 21:11

DavidW