diff options
Diffstat (limited to 'numpy/lib/tests/test_io.py')
-rw-r--r-- | numpy/lib/tests/test_io.py | 84 |
1 files changed, 78 insertions, 6 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 9f7a585ef..9e398cf23 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -14,24 +14,54 @@ class TestSaveTxt(NumpyTestCase): a =np.array( [[1,2],[3,4]], int) c = StringIO.StringIO() - np.savetxt(c, a) + np.savetxt(c, a, fmt='%d') c.seek(0) - assert(c.readlines(), ['1 2\n', '3 4\n']) + assert_equal(c.readlines(), ['1 2\n', '3 4\n']) def test_1D(self): a = np.array([1,2,3,4], int) c = StringIO.StringIO() np.savetxt(c, a, fmt='%d') c.seek(0) - assert(c.readlines(), ['1\n', '2\n', '3\n', '4\n']) + lines = c.readlines() + assert_equal(lines, ['1\n', '2\n', '3\n', '4\n']) def test_record(self): a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) c = StringIO.StringIO() np.savetxt(c, a, fmt='%d') c.seek(0) - assert(c.readlines(), ['1 2\n', '3 4\n']) - + assert_equal(c.readlines(), ['1 2\n', '3 4\n']) + def test_delimiter(self): + a = np.array([[1., 2.], [3., 4.]]) + c = StringIO.StringIO() + np.savetxt(c, a, delimiter=',', fmt='%d') + c.seek(0) + assert_equal(c.readlines(), ['1,2\n', '3,4\n']) + + +## def test_format(self): +## a = np.array([(1, 2), (3, 4)]) +## c = StringIO.StringIO() +## # Sequence of formats +## np.savetxt(c, a, fmt=['%02d', '%3.1f']) +## c.seek(0) +## assert_equal(c.readlines(), ['01 2.0\n', '03 4.0\n']) +## +## # A single multiformat string +## c = StringIO.StringIO() +## np.savetxt(c, a, fmt='%02d : %3.1f') +## c.seek(0) +## lines = c.readlines() +## assert_equal(lines, ['01 : 2.0\n', '03 : 4.0\n']) +## +## # Specify delimiter, should be overiden +## c = StringIO.StringIO() +## np.savetxt(c, a, fmt='%02d : %3.1f', delimiter=',') +## c.seek(0) +## lines = c.readlines() +## assert_equal(lines, ['01 : 2.0\n', '03 : 4.0\n']) + class TestLoadTxt(NumpyTestCase): def test_record(self): @@ -45,7 +75,6 @@ class TestLoadTxt(NumpyTestCase): d = StringIO.StringIO() d.write('M 64.0 75.0\nF 25.0 60.0') d.seek(0) - mydescriptor = {'names': ('gender','age','weight'), 'formats': ('S1', 'i4', 'f4')} @@ -54,6 +83,7 @@ class TestLoadTxt(NumpyTestCase): y = np.loadtxt(d, dtype=mydescriptor) assert_array_equal(y, b) + def test_array(self): c = StringIO.StringIO() c.write('1 2\n3 4') @@ -92,6 +122,48 @@ class TestLoadTxt(NumpyTestCase): converters={3:lambda s: int(s or -999)}) a = np.array([1,2,3,-999,5], int) assert_array_equal(x, a) + + def test_comments(self): + c = StringIO.StringIO() + c.write('# comment\n1,2,3,5\n') + c.seek(0) + x = np.loadtxt(c, dtype=int, delimiter=',', \ + comments='#') + a = np.array([1,2,3,5], int) + assert_array_equal(x, a) + + def test_skiprows(self): + c = StringIO.StringIO() + c.write('comment\n1,2,3,5\n') + c.seek(0) + x = np.loadtxt(c, dtype=int, delimiter=',', \ + skiprows=1) + a = np.array([1,2,3,5], int) + assert_array_equal(x, a) + + c = StringIO.StringIO() + c.write('# comment\n1,2,3,5\n') + c.seek(0) + x = np.loadtxt(c, dtype=int, delimiter=',', \ + skiprows=1) + a = np.array([1,2,3,5], int) + assert_array_equal(x, a) + + def test_usecols(self): + a =np.array( [[1,2],[3,4]], float) + c = StringIO.StringIO() + np.savetxt(c, a) + c.seek(0) + x = np.loadtxt(c, dtype=float, usecols=(1,)) + assert_array_equal(x, a[:,1]) + + a =np.array( [[1,2,3],[3,4,5]], float) + c = StringIO.StringIO() + np.savetxt(c, a) + c.seek(0) + x = np.loadtxt(c, dtype=float, usecols=(1,2)) + assert_array_equal(x, a[:,1:]) + class Testfromregex(NumpyTestCase): def test_record(self): |