Converting a numpy.ndarray to string
18,571
Solution 1
Use ndarray.tostring
-
my_string_numpy_array.tostring()
Sample output -
In [176]: my_string_numpy_array.tostring()
Out[176]: 'My name is Aman Raparia'
Solution 2
The right answer for numpy is Divakar's ndarray.tostring
.
An alternative is to use chr
on each array element and join together (for a non numpy array for example):
>>> ''.join([chr(e) for e in my_string_numpy_array])
'My name is Aman Raparia'
Author by
Aman Raparia
Working to Pro-actively resolve the problems in a Telecom using Using Data Analytics and Machine Learning. Weapons in armory Python, Pandas, numpy, sklearn, xgboost, trying for Deep learning, Django, Flask and ORM.
Updated on June 20, 2022Comments
-
Aman Raparia over 1 year
I have string
my_string = "My name is Aman Raparia"
which I converted to numpy array of ordinal values using the statement
my_string_numpy_array = np.fromstring(my_string, dtype=np.uint8)
Is there any way to get back the original string from the my_string_numpy_array?