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!
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With