Skip to content Skip to sidebar Skip to footer

Python Numpy.savetext A Matrix With Mixed Format

I am trying to save as text a matrix which has in each row 288 float and 1 string at the end, i've used the savetxt like this: np.savetxt('name', matrix, delimiter=' ', header='st

Solution 1:

Your error message says that you are providing string data (dtype ('<U32')) -U32 stands for Unicode string- but your format specifier is floating numbers followed by a string ('%3f(repeated 288 times without spaces)%s').

Since your matrix is already string there is no point in trying to format it anyway. If you are not satisfied with the floating point digits you should probably format it before entering to this matrix.

So, in your case to just write your present matrix just use:

np.savetxt('name', matrix, delimiter='  ', header='string', comments='', fmt='%s')

which will treat each element as a string (which they actually are) and write it to the text file.

Also maybe this answer provide some clues if you are not satisfied.

Post a Comment for "Python Numpy.savetext A Matrix With Mixed Format"