1

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

5
  • Tell us more about X (dtype for example). Have you explored the fmt parameter for savetxt? Commented Oct 25, 2015 at 19:52
  • Can you include an example of that this matrix contains? I just did X = np.matrix( np.random.uniform( 0,1, (4,4)) ); np.savetxt("X.csv", X, delimiter=",") and it works just fine. Commented Oct 25, 2015 at 19:53
  • @hpaulj Could u please explain how i should use fmt in this case? Commented Oct 25, 2015 at 20:01
  • I edited the question Commented Oct 25, 2015 at 20:04
  • How would you format your row sample? %s, %s, %d, %f,etc'%(row)`? Commented Oct 25, 2015 at 21:06

1 Answer 1

1

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')

Sign up to request clarification or add additional context in comments.

1 Comment

The '%s' fmt says - convert each column to its usual string representation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.