diff options
author | Stefan van der Walt <stefan@sun.ac.za> | 2008-07-22 15:12:12 +0000 |
---|---|---|
committer | Stefan van der Walt <stefan@sun.ac.za> | 2008-07-22 15:12:12 +0000 |
commit | b5e26c40f2766890b600bd8b68eb01b4ec9f902b (patch) | |
tree | c3249c5d2abc0b26481e345a60c0d3b8be94d8d8 /numpy/lib/io.py | |
parent | a2dcde587eb07398d0a30189a898c614ea1ba1aa (diff) | |
download | numpy-b5e26c40f2766890b600bd8b68eb01b4ec9f902b.tar.gz |
Clean up loadtxt. Fix Python 2.3 compatibility.
Diffstat (limited to 'numpy/lib/io.py')
-rw-r--r-- | numpy/lib/io.py | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py index 7ac154df5..e2c3933bf 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -274,6 +274,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, SeeAlso: scipy.io.loadmat to read and write matfiles. """ + user_converters = converters if _string_like(fname): if fname.endswith('.gz'): @@ -317,26 +318,26 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, # Read until we find a line with some values, and use # it to estimate the number of columns, N. - read_line = None - while not read_line: + first_vals = None + while not first_vals: first_line = fh.readline() - read_line = split_line(first_line) - N = len(usecols or read_line) + first_vals = split_line(first_line) + N = len(usecols or first_vals) dtype_types = flatten_dtype(dtype) if len(dtype_types) > 1: # We're dealing with a structured array, each field of # the dtype matches a column - converterseq = [_getconv(dt) for dt in dtype_types] + converters = [_getconv(dt) for dt in dtype_types] else: # All fields have the same dtype - converterseq = [defconv for i in xrange(N)] + converters = [defconv for i in xrange(N)] # By preference, use the converters specified by the user - for i, conv in (converters or {}).iteritems(): + for i, conv in (user_converters or {}).iteritems(): if usecols: i = usecols.find(i) - converterseq[i] = conv + converters[i] = conv # Parse each line, including the first for i, line in enumerate(itertools.chain([first_line], fh)): @@ -348,7 +349,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, vals = [vals[i] for i in usecols] # Convert each value according to its column and store - X.append(tuple(conv(val) for (conv, val) in zip(converterseq, vals))) + X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)])) if len(dtype_types) > 1: # We're dealing with a structured array, with a dtype such as |