Talia
Talia

Reputation: 3137

Error for saving a matrix

I have a matrix containing string, float and int numbers and i want to save it as a csv file. This is the command that i use:

numpy.savetxt("X.csv", X, delimiter=",")

Where X is name of the matrix. This is error:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
 File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1073, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: float argument required, not str

This is the first row of matrix:

16  disk    11  10.29   4.63    30.22 11  20.49   60.60   20.22 11  22.17   0.71    10.37

and type of matrix is numpy.ndarray.

How can i save it? Thanks

Upvotes: 1

Views: 739

Answers (1)

Julien
Julien

Reputation: 203

In the example you give, the matrix is composed of strings as well as floats. Then this question has already been answered here:

How to use python numpy.savetxt to write strings and float number to an ASCII file?

Edit your np.savetxt call to be numpy.savetxt("X.csv", X, delimiter=",", fmt='%s')

Upvotes: 1

Related Questions