문제

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