Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy bytes to plain string

Tags:

python

numpy

I have a numpy array X with dtype 'S' (numpy.bytes_). For example printing print(X[0, 0]) yields b'somestring'. Similarly str(X[0, 0]) returns string "b'somestring'".

However I need to print or convert to string so that it does not contain b' at the beginning and ' at the end. I just want to print somestring or return a string "somestring". How to do it?

Note: I cannot change the type of the array.

like image 762
V.K. Avatar asked May 12 '14 20:05

V.K.


People also ask

Which method is used to convert raw byte data to a string a Tostring () b convert () C encode () D decode ()?

Using the decode() method Python provides the built-in decode() method, which is used to convert bytes to a string. Let's understand the following example.

Which method is used to convert raw byte data to a string in Python?

Similarly, Decoding is process to convert a Byte object to String. It is implemented using decode() . A byte string can be decoded back into a character string, if you know which encoding was used to encode it.


1 Answers

You just need to decode the string back into ASCII, so it would just be:

bytes_string.decode('UTF-8') 

Demo:

>>> b'somestring'.decode('UTF-8') 'somestring' 
like image 65
anon582847382 Avatar answered Oct 18 '22 16:10

anon582847382