diff options
author | Stefan van der Walt <stefan@sun.ac.za> | 2008-09-22 20:28:00 +0000 |
---|---|---|
committer | Stefan van der Walt <stefan@sun.ac.za> | 2008-09-22 20:28:00 +0000 |
commit | f1855ef6c44ff037b606cbc0673a536ece31a945 (patch) | |
tree | d056013285f0c17abc2489b0c95861c6fcf3bdf6 | |
parent | 3060e39a4dc5ffc4852d7f7b2adc8f8d79bde0df (diff) | |
download | numpy-f1855ef6c44ff037b606cbc0673a536ece31a945.tar.gz |
Ignore unused converters in `loadtxt`.
-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() |