[Numpy-discussion] savetxt fmt argument fails when using a unicode string

Daπid davidmenhur at gmail.com
Wed Nov 13 11:54:27 EST 2013


The following works on Numpy 1.8.0:

from __future__ import unicode_literals
import numpy as np

np.savetxt('a.csv', [1], fmt=str('%.3f'))


Without the str, I get a clearer error:

Traceback (most recent call last):
  File "a.py", line 4, in <module>
    np.savetxt('a.csv', [1], fmt='%.3f')
  File ".virtualenv/local/lib/python2.7/site-packages/numpy/lib/npyio.py",
line 1049, in savetxt
    raise ValueError('invalid fmt: %r' % (fmt,))
ValueError: invalid fmt: u'%.3f'


/David.



On 13 November 2013 17:28, Pierre Haessig <pierre.haessig at crans.org> wrote:

> Hi,
>
> I just noticed (with numpy 1.7.1) that the following code
>
> import numpy as np
> np.savetxt('a.csv', [1], fmt=u'%.3f')
>
> fails with:
>
>    1045         else:
>    1046             for row in X:
> -> 1047                 fh.write(asbytes(format % tuple(row) + newline))
>    1048         if len(footer) > 0:
>    1049             footer = footer.replace('\n', '\n' + comments)
>
> UnboundLocalError: local variable 'format' referenced before assignment
>
> (of course, the simple solution is to remove the "u" prefix, but in my
> real code, I have a "from __future__ import unicode_literals")
>
> Since parts of the savetxt function were changed since that, I don't
> know if this bug is still applicable or not.
> I'm guessing that since Warren Weckesser commit
>
> https://github.com/numpy/numpy/commit/0d284764a855cae11699228ff1b81e6d18f38ed2
> the problem would be caught earlier with a much nicer ValueError.
>
> However, I still see
>
> isinstance(fmt, str): (on line 1035
> https://github.com/numpy/numpy/blob/master/numpy/lib/npyio.py#L1035 )
>
> How can I make the fmt argument work with unicode_literals ?
>
> best,
> Pierre
>
>
>
>
> _______________________________________________
> 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/20131113/f29b4763/attachment.html>


More information about the NumPy-Discussion mailing list