[Numpy-discussion] A question about dtype syntax

Warren Weckesser warren.weckesser at enthought.com
Wed Aug 31 10:49:45 EDT 2011


On Wed, Aug 31, 2011 at 9:24 AM, Jean-Baptiste Marquette <marquett at iap.fr>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)]
>  [('bs3000l.cat', 280.61389, -7.24097, 11490, 0.1923, 0.0747)]
>  [('bs3000m.cat', 280.77074, -7.08237, 13989, 0.2289, 0.1009)]
>  [('bs3000n.cat', 280.77228, -7.23563, 15811, 0.1767, 0.1327)]
>  [('bs3001k.cat', 280.95383, -7.10004, 7402, 0.2539, 0.0777)]
>  [('bs3001l.cat', 280.95495, -7.23409, 13840, 0.1463, 0.1008)]
>  [('bs3001m.cat', 281.1172, -7.08094, 9608, 0.2311, 0.1458)]
>  [('bs3001n.cat', 281.12447, -7.23398, 14030, 0.2538, 0.1022)]
>  [('bs3002k.cat', 280.62533, -7.47818, 593, 0.0291, 0.0237)]
>  [('bs3002l.cat', 280.61508, -7.60359, 9122, 0.0518, 0.0205)]
>  [('bs3002m.cat', 280.77209, -7.46262, 1510, 0.0415, 0.0302)]
>  [('bs3002n.cat', 280.77578, -7.60117, 14177, 0.0807, 0.0327)]
>  [('bs3003k.cat', 280.96463, -7.42967, 13506, 0.0305, 0.0225)]
>  [('bs3003l.cat', 280.95638, -7.58462, 17903, 0.0458, 0.0298)]
>  [('bs3003m.cat', 281.12729, -7.42516, 15676, 0.0879, 0.0446)]
>  [('bs3003n.cat', 281.1354, -7.58497, 16015, 0.0685, 0.0376)]
>  [('bs3004k.cat', 280.61148, -7.78976, 14794, 0.079, 0.0473)]
>  [('bs3004l.cat', 280.61791, -7.94186, 15455, 0.0818, 0.0727)]
>  [('bs3004m.cat', 280.78388, -7.78834, 14986, 0.0966, 0.0313)]
>  [('bs3004n.cat', 280.78261, -7.93932, 18713, 0.0925, 0.0472)]
>  [('bs3005k.cat', 280.9659, -7.78816, 14906, 0.0456, 0.022)]
>  [('bs3005l.cat', 280.96811, -7.93894, 19744, 0.021, 0.0218)]
>  [('bs3005m.cat', 281.1344, -7.78035, 15943, 0.0687, 0.0203)]
>  [('bs3005n.cat', 281.13915, -7.93027, 18183, 0.1173, 0.0695)]
>  [('bs3006k.cat', 280.61294, -8.14201, 13309, 0.143, 0.065)]
>  [('bs3006l.cat', 280.65109, -8.29416, 405, 0.258, 0.1147)]
>  [('bs3006m.cat', 280.78767, -8.13916, 14527, 0.1106, 0.0568)]
>  [('bs3006n.cat', 280.80935, -8.28823, 818, 0.2382, 0.0764)]
>  [('bs3007k.cat', 280.96614, -8.1401, 13251, 0.0946, 0.0415)]
>  [('bs3007l.cat', 280.97158, -8.23797, 5807, 0.1758, 0.0636)]
>  [('bs3007m.cat', 281.14129, -8.13799, 13886, 0.1524, 0.0517)]
>  [('bs3007n.cat', 281.15309, -8.2476, 214, 0.1584, 0.0648)]]
>
>

Notice that your array is actually a 2D structured array with shape (n, 1).
Try reshaping it to (n,) or apply np.squeeze before calling savetxt.

Warren



>
> 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:
>
> 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 974, in savetxt
>     % fmt)
> AttributeError: fmt has wrong number of % formats.  %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')
>
> Quite puzzling...
> Should I switch to the I/O of asciitable package ?
> Anyway, thanks again for your help.
> JB
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110831/4cc38188/attachment.html>


More information about the NumPy-Discussion mailing list