5

I have a numpy array of characters and when I write it to file it writes as:

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

I want it to write with just the letters and without the brackets or quotations i.e. as:

KRKPTTKTKRGL 

I have looked at numpy documentation and from what I have gathered the solution is a chararray however it looks like this is not as functional as a normal array.

Any help would be great. Thanks!

aatakan
  • 83
  • 1
  • 5

5 Answers5

9

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 str.join which accepts an iterable (list, numpy array, etc).

import numpy as np

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

s = ''.join(arr)
# KRKPTTKTKRGL
Ffisegydd
  • 43,058
  • 12
  • 125
  • 109
6

You can use tostring() method of numpy as:

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

Since you have a numpy array, this method will be faster than join().

For Python3x tostring() can be used as:

>>> st = np.array(['K','R','K','P','T','T','K','T','K','R','G','L'])
>>> st.astype('|S1').tostring().decode('utf-8')
'KRKPTTKTKRGL' 
Irshad Bhat
  • 7,301
  • 1
  • 19
  • 30
  • 1
    In Python 3 (don't know about 2) this creates a bytes object, not a string. Note that the OP's array will probably contain commas in the definition, as otherwise it is effectively `np.array(['KRKPTTKTKRGL'])`. I suspect the lack of commas is simply because that's what OP says is printed to a file. See the docs [here](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tostring.html) for more details. – Ffisegydd Dec 19 '14 at 19:16
  • I am using Python 2, this method worked perfectly. It did exactly what I wanted. Thanks! – aatakan Dec 20 '14 at 12:58
1

In a numpy way, 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, we convert the array into a usable unicode (check the kind attribute at page bottom) format of the size of the array.

Which is the dtype of the string if you try to convert it to 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.

Andrew Naguib
  • 3,978
  • 2
  • 21
  • 42
0
"".join(['K' 'R' 'K' 'P' 'T' 'T' 'K' 'T' 'K' 'R' 'G' 'L'])
ZdaR
  • 19,186
  • 6
  • 55
  • 76
0

If you use the tofile() method to save the array to file, the default separator is the empty string "".

So if your array is this,

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

then if you're using Python 2,

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

creates a file with the following content:

KRKPTTKTKRGL

If you're using Python 3, you may need to explicitly cast the array to the S string type first:

>>> st.astype('|S1').tofile('myfile.txt')
Alex Riley
  • 132,653
  • 39
  • 224
  • 205