Skip to content

Commit bf1e9b7

Browse files
authored
Merge pull request numpy#13409 from seberg/fix-unicode-fmt-savetxt
BUG: (py2 only) fix unicode support for savetxt fmt string
2 parents 5d248de + 59a521e commit bf1e9b7

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

numpy/lib/npyio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ def first_write(self, v):
13991399
if len(fmt) != ncol:
14001400
raise AttributeError('fmt has wrong shape. %s' % str(fmt))
14011401
format = asstr(delimiter).join(map(asstr, fmt))
1402-
elif isinstance(fmt, str):
1402+
elif isinstance(fmt, basestring):
14031403
n_fmt_chars = fmt.count('%')
14041404
error = ValueError('fmt has wrong number of %% formats: %s' % fmt)
14051405
if n_fmt_chars == 1:

numpy/lib/tests/test_io.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,19 @@ def test_unicode_stringstream(self):
561561
s.seek(0)
562562
assert_equal(s.read(), utf8 + '\n')
563563

564+
@pytest.mark.parametrize("fmt", [u"%f", b"%f"])
565+
@pytest.mark.parametrize("iotype", [StringIO, BytesIO])
566+
def test_unicode_and_bytes_fmt(self, fmt, iotype):
567+
# string type of fmt should not matter, see also gh-4053
568+
a = np.array([1.])
569+
s = iotype()
570+
np.savetxt(s, a, fmt=fmt)
571+
s.seek(0)
572+
if iotype is StringIO:
573+
assert_equal(s.read(), u"%f\n" % 1.)
574+
else:
575+
assert_equal(s.read(), b"%f\n" % 1.)
576+
564577

565578
class LoadTxtBase(object):
566579
def check_compressed(self, fopen, suffixes):

0 commit comments

Comments
 (0)