3

I am trying to use numpy.savetxt to dump data in to varieties of format for each column.

when data is

data = np.array([[1.111, 2.222, 3.333],
                 [4.444, 5.555, 6.666],
                 [7.777, 8.888, 9.999] ])
np.savetxt('data.txt', data, 
           fmt= ['%i', '%.2f', '%s'], ## <== 1st column, int, 2nd float, 3rd string
           delimiter = ',')

everything works fine. But when data is:

data = np.array([[1.111, 2.222, 'three'],
                 [4.444, 5.555, 'six'],
                 [7.777, 8.888, 'nine'] ])

np.savetxt('data.txt', data, 
           fmt= ['%i', '%.2f', '%s'], ## <== 1st column, int, 2nd float, 3rd string
           delimiter = ',')

it gives me a error that: fh.write(asbytes(format % tuple(row) + newline)) TypeError: %d format: a number is required, not numpy.string_

Anybody have a clue?

2 Answers 2

3

Looking at the array that you have created it looks like this:

array([['1.111', '2.222', 'three'],
   ['4.444', '5.555', 'six'],
   ['7.777', '8.888', 'nine']], 
  dtype='<U5')

As you can see all elements are strings and that is why you get that error. However, if you do something like this, it should work.

dt = np.dtype("f, f, U5")
data = np.array([(1.111, 2.222, 'three'), # notice that each row had to be a tuple in my case
             (4.444, 5.555, 'six'),
             (7.777, 8.888, 'nine')], dtype=dt)
np.savetxt('data.txt', data, 
           fmt= ['%i', '%.2f', '%s'], ## <== 1st column, int, 2nd float, 3rd string
           delimiter = ',')

More info about dtypes are here: Data Type Objects

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

3 Comments

what does "dtype = '<U5' " means? where you got this definition? In addtion, by the way, the first two columns is all float, not string.
Nice! I think I didn't define the data type for each column in advance. And the data in the array are not tuple. I think that's why.
'<' indicates the byte order. 'U' mean unicode characters. '5' means 5 unicode characters.
2

For integer the %d format option should be used instead of %i:

np.savetxt('test.txt', data, fmt=['%d', '%.2f', '%s'])

When loading the array you should also specify dtype properly:

np.genfromtxt('test.txt', dtype=[('col1', int), ('col2', float), ('col3', '|S8')])

2 Comments

np.savetxt(fname3, data3, fmt= ['%i', '%.2f', '%s'], delimiter = ','), it doens't work... but thank you.
@MacSanhe it was a typo... the right is ['%d', '%.2f', '%s']... I've updated the answer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.