Convert numpy.ndarray to string

I have a line

my_string = "My name is Aman Raparia"

      

which i converted to numpy array of ordinal values ​​using operator

my_string_numpy_array = np.fromstring(my_string, dtype=np.uint8)

      

Is there a way to get the original string back from my_string_numpy_array?

+3


source to share


2 answers


Use ndarray.tostring

-

my_string_numpy_array.tostring()

      



Sample output -

In [176]: my_string_numpy_array.tostring()
Out[176]: 'My name is Aman Raparia'

      

+4


source


Correct answer Divakar ndarray.tostring

.

An alternative is to use chr

for each element of the array and concatenate (for example for an array without numpy):



>>> ''.join([chr(e) for e in my_string_numpy_array])
'My name is Aman Raparia'

      

+1


source







All Articles