diff options
-rw-r--r-- | numpy/lib/io.py | 6 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 11 |
2 files changed, 16 insertions, 1 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py index 93c955942..caa790809 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -378,7 +378,11 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, # By preference, use the converters specified by the user for i, conv in (user_converters or {}).iteritems(): if usecols: - i = usecols.index(i) + try: + i = usecols.index(i) + except ValueError: + # Unused converter specified + continue converters[i] = conv # Parse each line, including the first diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 9cccbe041..d257b5423 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -258,6 +258,17 @@ class TestLoadTxt(TestCase): c = StringIO.StringIO() assert_raises(IOError, np.loadtxt, c) + def test_unused_converter(self): + c = StringIO.StringIO() + c.writelines(['1 21\n', '3 42\n']) + c.seek(0) + data = np.loadtxt(c, usecols=(1,), converters={0: lambda s: int(s, 16)}) + assert_array_equal(data, [21, 42]) + + c.seek(0) + data = np.loadtxt(c, usecols=(1,), converters={1: lambda s: int(s, 16)}) + assert_array_equal(data, [33, 66]) + class Testfromregex(TestCase): def test_record(self): c = StringIO.StringIO() |