From c3e4bbb5dc944081bb84d5a42089237576c647d3 Mon Sep 17 00:00:00 2001 From: Paul Anton Letnes Date: Mon, 4 Apr 2011 16:55:16 +0200 Subject: [PATCH 1/6] moved import statement in npyio.py for ease of reading --- numpy/lib/npyio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 306b9ce0790f..c74157ea20a8 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -6,6 +6,7 @@ import format import sys import os +import re import sys import itertools import warnings @@ -975,7 +976,6 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'): if own_fh: fh.close() -import re def fromregex(file, regexp, dtype): """ Construct an array from a text file, using regular expression parsing. From ef3915a0b431ea74f3346d849f735a22d417b362 Mon Sep 17 00:00:00 2001 From: Paul Anton Letnes Date: Thu, 28 Jul 2011 22:50:58 +0200 Subject: [PATCH 2/6] Ticket #1793 patch: genfromtxt now returns an empty array if the input file is empty. The unit test has been rewritten to check for this and the test is passed. --- numpy/lib/npyio.py | 5 +++-- numpy/lib/tests/test_io.py | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index f7cde270d4a2..2e8829613e1c 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1274,8 +1274,9 @@ 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 = [] # 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 f9da258dc583..23b3a34031f1 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -959,12 +959,12 @@ def test_usecols_with_named_columns(self): usecols=('a', 'c'), **kwargs) assert_equal(test, ctrl) - def test_empty_file(self): "Test that an empty file raises the proper exception" data = StringIO() - assert_raises(IOError, np.ndfromtxt, data) - + test = np.genfromtxt(data) + ctrl = np.array([]) + assert_equal(test, ctrl) def test_fancy_dtype_alt(self): "Check that a nested dtype isn't MIA" From abcbb6ad8ca5b481d04a805454dd4323e2051537 Mon Sep 17 00:00:00 2001 From: Paul Anton Letnes Date: Thu, 28 Jul 2011 23:24:39 +0200 Subject: [PATCH 3/6] Added warning for empty files in genfromtxt (ticket 1793 related) --- numpy/lib/npyio.py | 1 + 1 file changed, 1 insertion(+) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 2e8829613e1c..548e2e347362 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1277,6 +1277,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # return an empty array if the datafile is empty first_line = '' first_values = [] + warnings.warn('genfromtxt: Empty input file.') # Should we take the first values as names ? if names is True: From 5f51e6a6cb837a40dbd3bf991ff5c1b484822504 Mon Sep 17 00:00:00 2001 From: Paul Anton Letnes Date: Thu, 28 Jul 2011 23:41:05 +0200 Subject: [PATCH 4/6] Ticket 1793 related again - added filename to warning in case of empty file in genfromtxt --- numpy/lib/npyio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 548e2e347362..10a15421329d 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1277,7 +1277,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # return an empty array if the datafile is empty first_line = '' first_values = [] - warnings.warn('genfromtxt: Empty input file.') + warnings.warn('genfromtxt: Empty input file: "{0}"'.format(fname)) # Should we take the first values as names ? if names is True: From 559399d013d0ff072f76e31afb4ac28e87c2e19e Mon Sep 17 00:00:00 2001 From: Paul Anton Letnes Date: Fri, 29 Jul 2011 07:58:59 +0200 Subject: [PATCH 5/6] Ticket 1793 again - removed use of the .format; using % operator instead. also, removed an unneeded line of code in corresponding unit test. --- numpy/lib/npyio.py | 2 +- numpy/lib/tests/test_io.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 10a15421329d..44a91c0c7995 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1277,7 +1277,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # return an empty array if the datafile is empty first_line = '' first_values = [] - warnings.warn('genfromtxt: Empty input file: "{0}"'.format(fname)) + 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 23b3a34031f1..55068b7ab561 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -963,8 +963,7 @@ def test_empty_file(self): "Test that an empty file raises the proper exception" data = StringIO() test = np.genfromtxt(data) - ctrl = np.array([]) - assert_equal(test, ctrl) + assert_equal(test, np.array([])) def test_fancy_dtype_alt(self): "Check that a nested dtype isn't MIA" From 561689e8616093de8716b848fc6ccd33609a8471 Mon Sep 17 00:00:00 2001 From: Paul Anton Letnes Date: Sun, 31 Jul 2011 17:18:25 +0200 Subject: [PATCH 6/6] Regarding Ticket #1236: added header, footer, and comment keyword args. --- numpy/lib/npyio.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 44a91c0c7995..fe00b3e79475 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -833,7 +833,8 @@ def split_line(line): return X -def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'): +def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', + footer='', commentstr=''): """ Save an array to a text file. @@ -853,6 +854,13 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'): Character separating columns. newline : str .. versionadded:: 1.5.0 + header : str + String that will be written to the beginning of the file. (Remember to + add a newline, ``'\n'``, at the end of the line, if desired.) + footer : str + String that will be written to the end of the file. + commentstr : str + String that will be prepended to the ``header`` and ``footer`` strings. Character separating lines. @@ -976,8 +984,10 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'): else: format = fmt + fh.write(asbytes(commentstr + header)) for row in X: fh.write(asbytes(format % tuple(row) + newline)) + fh.write(asbytes(commentstr + footer)) finally: if own_fh: fh.close()