diff options
author | Paul Anton Letnes <paul.anton.letnes@gmail.com> | 2011-07-28 22:50:58 +0200 |
---|---|---|
committer | Ralf Gommers <ralf.gommers@googlemail.com> | 2011-07-31 13:09:38 +0200 |
commit | 72ab385d17d9067f97652aeae87a820f7de41298 (patch) | |
tree | 67415466f8bc09ce0323fd69fe692214ca09e163 /numpy/lib | |
parent | 2acb9282bc991c4d3789bf2f7be8a2aaf1859df0 (diff) | |
download | numpy-72ab385d17d9067f97652aeae87a820f7de41298.tar.gz |
ENH: let genfromtxt return empty array for empty input file instead of an error.
A warning for empty files is issued, including file name. Closes #1793.
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/npyio.py | 6 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 11 |
2 files changed, 10 insertions, 7 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index f7cde270d..44a91c0c7 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1274,8 +1274,10 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, first_line = asbytes('').join(first_line.split(comments)[1:]) first_values = split_line(first_line) except StopIteration: - # might want to return empty array instead of raising error. - raise IOError('End-of-file reached before encountering data.') + # return an empty array if the datafile is empty + first_line = '' + first_values = [] + warnings.warn('genfromtxt: Empty input file: "%s"' % fname) # Should we take the first values as names ? if names is True: diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index f9da258dc..18585375e 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -5,6 +5,7 @@ import threading from tempfile import mkstemp, NamedTemporaryFile import time from datetime import datetime +import warnings import numpy as np import numpy.ma as ma @@ -637,7 +638,6 @@ class TestFromTxt(TestCase): assert_equal(test, ctrl) def test_skip_footer_with_invalid(self): - import warnings basestr = '1 1\n2 2\n3 3\n4 4\n5 \n6 \n7 \n' warnings.filterwarnings("ignore") # Footer too small to get rid of all invalid values @@ -959,12 +959,12 @@ M 33 21.99 usecols=('a', 'c'), **kwargs) assert_equal(test, ctrl) - def test_empty_file(self): - "Test that an empty file raises the proper exception" + "Test that an empty file raises the proper warning." + warnings.filterwarnings("ignore", message="genfromtxt: Empty input file:") data = StringIO() - assert_raises(IOError, np.ndfromtxt, data) - + test = np.genfromtxt(data) + assert_equal(test, np.array([])) def test_fancy_dtype_alt(self): "Check that a nested dtype isn't MIA" @@ -1440,5 +1440,6 @@ def test_npzfile_dict(): assert_('x' in list(z.iterkeys())) + if __name__ == "__main__": run_module_suite() |