Question

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.

Was it helpful?

Solution

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'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top