python numpy savetxt

numpy, python

Solution

You need to construct you array differently:

z = np.array(zip([1,2,3,4,5], ['a','b','c','d','e']), dtype=[('int', int), ('str', '|S1')])
np.savetxt('test.txt', z, fmt='%i %s')

when you're passing a sequence, `savetext` performs `asarray(sequence)` call and resulting array is of type `|S4`, that is all elements are strings! that's why you see this error.

Problem

Can someone indicate what I am doing wrong here? ``` import numpy as np a = np.array([1,2,3,4,5],dtype=int) b = np.array(['a','b','c','d','e'],dtype='|S1') np.savetxt('test.txt',zip(a,b),fmt="%i %s") ``` The output is: ``` Traceback (most recent call last): File "loadtxt.py", line 6, in <module> np.savetxt('test.txt',zip(a,b),fmt="%i %s") File "/Users/tom/Library/Python/2.6/site-packages/numpy/lib/io.py", line 785, in savetxt fh.write(format % tuple(row) + '\n') TypeError: %d format: a number is required, not numpy.string_ ```

Original source