def loadtxt(fname, dtype=float, comments='<STR_LIT:#>', delimiter=None,<EOL>converters=None, skiprows=<NUM_LIT:0>, usecols=None, unpack=False,<EOL>ndmin=<NUM_LIT:0>):
<EOL>if comments is not None:<EOL><INDENT>comments = asbytes(comments)<EOL><DEDENT>user_converters = converters<EOL>if delimiter is not None:<EOL><INDENT>delimiter = asbytes(delimiter)<EOL><DEDENT>if usecols is not None:<EOL><INDENT>usecols = list(usecols)<EOL><DEDENT>fown = False<EOL>try:<EOL><INDENT>if _is_string_like(fname):<EOL><INDENT>fown = True<EOL>if fname.endswith('<STR_LIT>'):<EOL><INDENT>fh = iter(seek_gzip_factory(fname))<EOL><DEDENT>elif fname.endswith('<STR_LIT>'):<EOL><INDENT>import bz2<EOL>fh = iter(bz2.BZ2File(fname))<EOL><DEDENT>elif sys.version_info[<NUM_LIT:0>] == <NUM_LIT:2>:<EOL><INDENT>fh = iter(open(fname, '<STR_LIT>'))<EOL><DEDENT>else:<EOL><INDENT>fh = iter(open(fname))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fh = iter(fname)<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>X = []<EOL>def flatten_dtype(dt):<EOL><INDENT>"""<STR_LIT>"""<EOL>if dt.names is None:<EOL><INDENT>shape = dt.shape<EOL>if len(shape) == <NUM_LIT:0>:<EOL><INDENT>return ([dt.base], None)<EOL><DEDENT>else:<EOL><INDENT>packing = [(shape[-<NUM_LIT:1>], list)]<EOL>if len(shape) > <NUM_LIT:1>:<EOL><INDENT>for dim in dt.shape[-<NUM_LIT:2>::-<NUM_LIT:1>]:<EOL><INDENT>packing = [(dim*packing[<NUM_LIT:0>][<NUM_LIT:0>], packing*dim)]<EOL><DEDENT><DEDENT>return ([dt.base] * int(np.prod(dt.shape)), packing)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>types = []<EOL>packing = []<EOL>for field in dt.names:<EOL><INDENT>tp, bytes = dt.fields[field]<EOL>flat_dt, flat_packing = flatten_dtype(tp)<EOL>types.extend(flat_dt)<EOL>if len(tp.shape) > <NUM_LIT:0>:<EOL><INDENT>packing.extend(flat_packing)<EOL><DEDENT>else:<EOL><INDENT>packing.append((len(flat_dt), flat_packing))<EOL><DEDENT><DEDENT>return (types, packing)<EOL><DEDENT><DEDENT>def pack_items(items, packing):<EOL><INDENT>"""<STR_LIT>"""<EOL>if packing is None:<EOL><INDENT>return items[<NUM_LIT:0>]<EOL><DEDENT>elif packing is tuple:<EOL><INDENT>return tuple(items)<EOL><DEDENT>elif packing is list:<EOL><INDENT>return list(items)<EOL><DEDENT>else:<EOL><INDENT>start = <NUM_LIT:0><EOL>ret = []<EOL>for length, subpacking in packing:<EOL><INDENT>ret.append(pack_items(items[start:start+length], subpacking))<EOL>start += length<EOL><DEDENT>return tuple(ret)<EOL><DEDENT><DEDENT>def split_line(line):<EOL><INDENT>"""<STR_LIT>"""<EOL>if comments is None:<EOL><INDENT>line = asbytes(line).strip(asbytes('<STR_LIT:\r\n>'))<EOL><DEDENT>else:<EOL><INDENT>line = asbytes(line).split(comments)[<NUM_LIT:0>].strip(asbytes('<STR_LIT:\r\n>'))<EOL><DEDENT>if line:<EOL><INDENT>return line.split(delimiter)<EOL><DEDENT>else:<EOL><INDENT>return []<EOL><DEDENT><DEDENT>try:<EOL><INDENT>dtype = np.dtype(dtype)<EOL>defconv = _getconv(dtype)<EOL>for i in range(skiprows):<EOL><INDENT>next(fh)<EOL><DEDENT>first_vals = None<EOL>try:<EOL><INDENT>while not first_vals:<EOL><INDENT>first_line = next(fh)<EOL>first_vals = split_line(first_line)<EOL><DEDENT><DEDENT>except StopIteration:<EOL><INDENT>first_line = '<STR_LIT>'<EOL>first_vals = []<EOL>warnings.warn('<STR_LIT>' % fname)<EOL><DEDENT>N = len(usecols or first_vals)<EOL>dtype_types, packing = flatten_dtype(dtype)<EOL>if len(dtype_types) > <NUM_LIT:1>:<EOL><INDENT>converters = [_getconv(dt) for dt in dtype_types]<EOL><DEDENT>else:<EOL><INDENT>converters = [defconv for i in range(N)]<EOL>if N > <NUM_LIT:1>:<EOL><INDENT>packing = [(N, tuple)]<EOL><DEDENT><DEDENT>for i, conv in (user_converters or {}).items():<EOL><INDENT>if usecols:<EOL><INDENT>try:<EOL><INDENT>i = usecols.index(i)<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>converters[i] = conv<EOL><DEDENT>for i, line in enumerate(itertools.chain([first_line], fh)):<EOL><INDENT>vals = split_line(line)<EOL>if len(vals) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>if usecols:<EOL><INDENT>vals = [vals[i] for i in usecols]<EOL><DEDENT>if len(vals) != N:<EOL><INDENT>line_num = i + skiprows + <NUM_LIT:1><EOL>raise ValueError("<STR_LIT>"<EOL>% line_num)<EOL><DEDENT>items = [conv(val) for (conv, val) in zip(converters, vals)]<EOL>items = pack_items(items, packing)<EOL>X.append(items)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>if fown:<EOL><INDENT>fh.close()<EOL><DEDENT><DEDENT>X = np.array(X, dtype)<EOL>if X.ndim == <NUM_LIT:3> and X.shape[:<NUM_LIT:2>] == (<NUM_LIT:1>, <NUM_LIT:1>):<EOL><INDENT>X.shape = (<NUM_LIT:1>, -<NUM_LIT:1>)<EOL><DEDENT>if ndmin not in [<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>]:<EOL><INDENT>raise ValueError('<STR_LIT>' % ndmin)<EOL><DEDENT>if X.ndim > ndmin:<EOL><INDENT>X = np.squeeze(X)<EOL><DEDENT>if X.ndim < ndmin:<EOL><INDENT>if ndmin == <NUM_LIT:1>:<EOL><INDENT>X = np.atleast_1d(X)<EOL><DEDENT>elif ndmin == <NUM_LIT:2>:<EOL><INDENT>X = np.atleast_2d(X).T<EOL><DEDENT><DEDENT>if unpack:<EOL><INDENT>if len(dtype_types) > <NUM_LIT:1>:<EOL><INDENT>return [X[field] for field in dtype.names]<EOL><DEDENT>else:<EOL><INDENT>return X.T<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return X<EOL><DEDENT>
Load data from a text file.

Each row in the text file must have the same number of values.

Parameters
----------
fname : file or str
    File, filename, or generator to read.  If the filename extension is
    ``.gz`` or ``.bz2``, the file is first decompressed. Note that
    generators should return byte strings for Python 3k.
dtype : data-type, optional
    Data-type of the resulting array; default: float.  If this is a
    record data-type, the resulting array will be 1-dimensional, and
    each row will be interpreted as an element of the array.  In this
    case, the number of columns used must match the number of fields in
    the data-type.
comments : str, optional
    The character used to indicate the start of a comment;
    default: '#'.
delimiter : str, optional
    The string used to separate values.  By default, this is any
    whitespace.
converters : dict, optional
    A dictionary mapping column number to a function that will convert
    that column to a float.  E.g., if column 0 is a date string:
    ``converters = {0: datestr2num}``.  Converters can also be used to
    provide a default value for missing data (but see also `genfromtxt`):
    ``converters = {3: lambda s: float(s.strip() or 0)}``.  Default: None.
skiprows : int, optional
    Skip the first `skiprows` lines; default: 0.
usecols : sequence, optional
    Which columns to read, with 0 being the first.  For example,
    ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns.
    The default, None, results in all columns being read.
unpack : bool, optional
    If True, the returned array is transposed, so that arguments may be
    unpacked using ``x, y, z = loadtxt(...)``.  When used with a record
    data-type, arrays are returned for each field.  Default is False.
ndmin : int, optional
    The returned array will have at least `ndmin` dimensions.
    Otherwise mono-dimensional axes will be squeezed.
    Legal values: 0 (default), 1 or 2.

    .. versionadded:: 1.6.0

Returns
-------
out : ndarray
    Data read from the text file.

See Also
--------
load, fromstring, fromregex
genfromtxt : Load data with missing values handled as specified.
scipy.io.loadmat : reads MATLAB data files

Notes
-----
This function aims to be a fast reader for simply formatted files.  The
`genfromtxt` function provides more sophisticated handling of, e.g.,
lines with missing values.

Examples
--------
>>> from StringIO import StringIO   # StringIO behaves like a file object
>>> c = StringIO("0 1\\n2 3")
>>> np.loadtxt(c)
array([[ 0.,  1.],
       [ 2.,  3.]])

>>> d = StringIO("M 21 72\\nF 35 58")
>>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
...                      'formats': ('S1', 'i4', 'f4')})
array([('M', 21, 72.0), ('F', 35, 58.0)],
      dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])

>>> c = StringIO("1,0,2\\n3,0,4")
>>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
>>> x
array([ 1.,  3.])
>>> y
array([ 2.,  4.])