From a2dcde587eb07398d0a30189a898c614ea1ba1aa Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 22 Jul 2008 06:37:48 +0000 Subject: Apply Stefan's patch for Ryan's loadtext fix. --- numpy/lib/io.py | 112 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 34 deletions(-) (limited to 'numpy/lib/io.py') diff --git a/numpy/lib/io.py b/numpy/lib/io.py index b1ae192ec..7ac154df5 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -10,6 +10,7 @@ import format import cStringIO import tempfile import os +import itertools from cPickle import load as _cload, loads from _datasource import DataSource @@ -286,44 +287,87 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, raise ValueError('fname must be a string or file handle') X = [] - dtype = np.dtype(dtype) - defconv = _getconv(dtype) - converterseq = None - if converters is None: - converters = {} - if dtype.names is not None: - if usecols is None: - converterseq = [_getconv(dtype.fields[name][0]) \ - for name in dtype.names] - else: - converters.update([(col,_getconv(dtype.fields[name][0])) \ - for col,name in zip(usecols, dtype.names)]) - - for i,line in enumerate(fh): - if i 1: + # We're dealing with a structured array, each field of + # the dtype matches a column + converterseq = [_getconv(dt) for dt in dtype_types] + else: + # All fields have the same dtype + converterseq = [defconv for i in xrange(N)] + + # By preference, use the converters specified by the user + for i, conv in (converters or {}).iteritems(): + if usecols: + i = usecols.find(i) + converterseq[i] = conv + + # Parse each line, including the first + for i, line in enumerate(itertools.chain([first_line], fh)): + vals = split_line(line) + if len(vals) == 0: + continue + + if usecols: + 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))) + + if len(dtype_types) > 1: + # We're dealing with a structured array, with a dtype such as + # [('x', int), ('y', [('s', int), ('t', float)])] + # + # First, create the array using a flattened dtype: + # [('x', int), ('s', int), ('t', float)] + # + # Then, view the array using the specified dtype. + X = np.array(X, dtype=np.dtype([('', t) for t in dtype_types])) + X = X.view(dtype) + else: + X = np.array(X, dtype) + + X = np.squeeze(X) + if unpack: + return X.T + else: + return X def savetxt(fname, X, fmt='%.18e',delimiter=' '): -- cgit v1.2.1