Turn NumPy Array of Characters to String

I have a numeric character array and when I write it to a file it is written as:

['K' 'R' 'K' 'P' 'T' 'T' 'K' 'T' 'K' 'R' 'G' 'L']

      

I want him to write only letters and no brackets or quotes, for example:

KRKPTTKTKRGL 

      

I looked at the numpy documentation and from what I put together is chararray, but it looks like it's not as functional as a regular array.

Any help would be great. Thank!

+5


source to share


5 answers


You can use the tostring()

method numpy

like:

>>> st = np.array(['K' 'R' 'K' 'P' 'T' 'T' 'K' 'T' 'K' 'R' 'G' 'L'])
>>> st.tostring()
'KRKPTTKTKRGL'

      

Since you have an array numpy

, this method will be faster than join()

.



For Python3x tostring()

can be used like:

>>> st = np.array(['K','R','K','P','T','T','K','T','K','R','G','L'])
>>> st.astype('|S1').tostring().decode('utf-8')
'KRKPTTKTKRGL' 

      

+5


source


If you just have a numpy array, then why not convert it to a string directly for writing to your file? You can do this using which accepts an iterable (list, numy array, etc.). str.join



import numpy as np

arr = np.array(['K', 'R', 'K', 'P', 'T', 'T', 'K', 'T', 'K', 'R', 'G', 'L'])

s = ''.join(arr)
# KRKPTTKTKRGL

      

+8


source


"".join(['K' 'R' 'K' 'P' 'T' 'T' 'K' 'T' 'K' 'R' 'G' 'L'])

      

+1


source


If you use the method tofile()

to save an array to a file, the default separator is an empty string ""

.

So, if your array is,

st = np.array(['K', 'R', 'K', 'P', 'T', 'T', 'K', 'T', 'K', 'R', 'G', 'L'])

      

then if you are using Python 2,

>>> st.tofile('myfile.txt')

      

creates a file with the following content:

KRKPTTKTKRGL

      

If you are using Python 3, you may need to explicitly cast the array to the string type S

:

>>> st.astype('|S1').tofile('myfile.txt')

      

0


source


Lonely, you can do:

Using F-string (only available for Python 3.4+)

s = arr.view(f'U{arr.size}')[0]

      

Using the default string:

s = arr.view('U' + str(arr.size))[0]

      

In both cases, we are converting the array to usable unicode (check the attribute kind

at the bottom of the page)
array size format.

What string if you are trying to convert it to dtype

numpy.array

In[15]: import numpy as np
In[16]: arr = np.array(['KRKPTTKTKRGL'])
In[17]: arr.dtype
Out[17]: dtype('<U12')

      

Note: it works with non-english letters.

0


source







All Articles