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.

有帮助吗?

解决方案

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'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top