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.

1 Answers1

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)
johnashu
  • 2,167
  • 4
  • 19
  • 44