Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write object array to .txt file

Tags:

python

numpy

When I do

k=12

rsf = np.zeros((int(k), 9), dtype='object')

for i in range(0, int(k)):
    rsf[i, 0] = "FREQ"
    for j in range(1, 9):
        rsf[i, j] = sampled[8*i+j-1, 0]

and then try to write it by

np.savetxt('test.txt', rsf, delimiter=',')

I get an error Mismatch between array dtype ('object') and format specifier

Any help on how I can overcome this issue? (And maybe append to arrays of non equal sizes?)

like image 235
Kostas Belivanis Avatar asked Jun 08 '16 21:06

Kostas Belivanis


People also ask

How do I load an array into a file?

Overview. Loading an array from a text file requires several steps, including: opening the file, reading the records, parsing (splitting) the records into fields, adding the fields to an array, and closing the file. The file may be read all at once and then parsed, or processed line by line.


1 Answers

More of the error message:

-> 1162                                     % (str(X.dtype), format))
   1163         if len(footer) > 0:
   1164             footer = footer.replace('\n', '\n' + comments)

TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e,%.18e')

savetxt is iterating over the rows of rsf, and for each trying to create a string that it can write to the file. Without fmt specification from you it tries a default format repeated by the number of columns. That's the format specifier.

This is a basic Python string formatting issue.

In [264]: row=rsf[1,:]

In [265]: row
Out[265]: array(['FREQ', 8, 9, 10, 11, 12, 13, 14, 15], dtype=object)

In [266]: '%s, %d, %d, %d, %d, %d, %d, %d, %d'%tuple(row)
Out[266]: 'FREQ, 8, 9, 10, 11, 12, 13, 14, 15'

so you need to call savetxt with something like:

In [267]: fmt='%s, %d, %d, %d, %d, %d, %d, %d, %d'

In [268]: np.savetxt('test.txt',rsf,fmt=fmt)

In [269]: cat test.txt
FREQ, 0, 1, 2, 3, 4, 5, 6, 7
FREQ, 8, 9, 10, 11, 12, 13, 14, 15
FREQ, 16, 17, 18, 19, 20, 21, 22, 23
FREQ, 24, 25, 26, 27, 28, 29, 30, 31
...

or you could simplify the format with the generic '%s'

In [270]: np.savetxt('test.txt',rsf,fmt='%5s',delimiter=',')

In [271]: cat test.txt
 FREQ,    0,    1,    2,    3,    4,    5,    6,    7
 FREQ,    8,    9,   10,   11,   12,   13,   14,   15
 FREQ,   16,   17,   18,   19,   20,   21,   22,   23
 FREQ,   24,   25,   26,   27,   28,   29,   30,   31
 FREQ,   32,   33,   34,   35,   36,   37,   38,   39
like image 161
hpaulj Avatar answered Sep 27 '22 16:09

hpaulj