[Numpy-discussion] A question about dtype syntax

Pierre GM pgmdevlist@gmail....
Wed Aug 31 09:33:26 CDT 2011

On Aug 31, 2011, at 4:24 PM, Jean-Baptiste Marquette wrote:

> Hi Pierre,
>> On Aug 31, 2011, at 3:40 PM, Jean-Baptiste Marquette wrote:
>>> Traceback (most recent call last):
>>>  File "/Users/marquett/workspace/Distort/src/StatsSep.py", line 44, in <module>
>>>    np.savetxt(Table, StatsAll, delimiter=' ', fmt=['%15s %.5f %.5f %5d %.4f %.4f'])
>>>  File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/numpy/lib/npyio.py", line 979, in savetxt
>>>    fh.write(asbytes(format % tuple(row) + newline))
>>> TypeError: not enough arguments for format string
>> Without knowing StatsAll, it ain't easy… From the exception message, we could expect that one of rows is empty or as less than the 6 elements required by your format string.
>> If you're using IPython, switch to debugger mode (pdb), then inspect row and format to find out the content of the offending line.
> Here is a (short) sample of StatsAll:
> [[('bs3000k.cat', 280.60341, -7.09118, 9480, 0.2057, 0.14)]

Have you tried the debugger as I suggested ? There must be a line somewhere that doesn't follow the format (the first one?)

>>> I struggled with various unsuccessful fmt syntaxes, and the numpy doc is very discrete about that topic:
>>> fmt : string or sequence of strings
>>>    A single format (%10.5f), a sequence of formats
>> Looks clear enough to me… But yes, a comment in the code shows that "   `fmt` can be a string with multiple insertion points or a list of formats.  E.g. '%10.5f\t%10d' or ('%10.5f', '$10d')" (so we should probably update the doc to this regard)
> The command with parentheses:
>         np.savetxt(Table, StatsAll, delimiter=' ', fmt=('%15s %.5f %.5f %5d %.4f %.4f'))
> fails as well, but with a different error:

Well, either you use 1 string 
fmt="%15s %.5f %.5f %5d %.4f %.4f"
or you use a list of strings 
fmt=("%15s", "%.5f", "%.5f", "%5d", "%.4f", "%.4f")

> Plus, this one:
>         np.savetxt(Table, StatsAll, delimiter=' ', fmt=('%15s', '%.5f', '%.5f', '%5d', '%.4f', '%.4f'))
> yields:
> Traceback (most recent call last):
>   File "/Users/marquett/workspace/Distort/src/StatsSep.py", line 44, in <module>
>     np.savetxt(Table, StatsAll, delimiter=' ', fmt=('%15s', '%.5f', '%.5f', '%5d', '%.4f', '%.4f'))
>   File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/numpy/lib/npyio.py", line 966, in savetxt
>     raise AttributeError('fmt has wrong shape.  %s' % str(fmt))
> AttributeError: fmt has wrong shape.  ('%15s', '%.5f', '%.5f', '%5d', '%.4f', '%.4f')

fmt=[('%15s', '%.5f', '%.5f', '%5d', '%.4f', '%.4f')]

> Quite puzzling...
> Should I switch to the I/O of asciitable package ?

As you wish. The easiest might be to write the file yourself.:
>>> fmt = "%15s %.5f %.5f %5d %.4f %.4f\n"
>>> f=open(Table,'r')
>>> for line in StatsAll:
>>>    f.write(fmt % line)
>>> f.close()
or something like that

More information about the NumPy-Discussion mailing list