1

I'm having trouble converting a numpy array of type bytes8 into a string. I guess I need to decode the data but I'm not sure how to do this for a numpy array. The array is of size [1200,8] but the data is repeated along the largest dimension so I only really need to convert one row to a string.

The first entry of the array gives the following output on the command line: array([b'5', b'5', b'5', b'7', b'0', b' ', b' ', b' '], dtype='|S1')

I have tried things like .tostring() but with no luck. The issue seems to be that the object is a numpy array.

0

1 Answer 1

1

Use a list comprehension including encoding="utf-8", otherwise the b will still remain in the list.

array = [str(s, encoding='UTF-8') for s in array]

returns

['5', '5', '5', '7', '0', ' ', ' ', ' ']

if you then wish to make a complete string you can do

''.join(array)

which will return the string

55570

You can achieve the same using only one line and parenthesis which will create a generator object.

A generator object will not create a list in memory so it will be faster.

array = ''.join(str(s, encoding='UTF-8') for s in array)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.