def genfromtxt(fname, dtype=float, comments='<STR_LIT:#>', delimiter=None,<EOL>skiprows=<NUM_LIT:0>, skip_header=<NUM_LIT:0>, skip_footer=<NUM_LIT:0>, converters=None,<EOL>missing='<STR_LIT>', missing_values=None, filling_values=None,<EOL>usecols=None, names=None,<EOL>excludelist=None, deletechars=None, replace_space='<STR_LIT:_>',<EOL>autostrip=False, case_sensitive=True, defaultfmt="<STR_LIT>",<EOL>unpack=None, usemask=False, loose=True, invalid_raise=True):
<EOL>if comments is not None:<EOL><INDENT>comments = asbytes(comments)<EOL><DEDENT>if isinstance(delimiter, unicode):<EOL><INDENT>delimiter = asbytes(delimiter)<EOL><DEDENT>if isinstance(missing, unicode):<EOL><INDENT>missing = asbytes(missing)<EOL><DEDENT>if isinstance(missing_values, (unicode, list, tuple)):<EOL><INDENT>missing_values = asbytes_nested(missing_values)<EOL><DEDENT>if usemask:<EOL><INDENT>from numpy.ma import MaskedArray, make_mask_descr<EOL><DEDENT>user_converters = converters or {}<EOL>if not isinstance(user_converters, dict):<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % type(user_converters))<EOL><DEDENT>own_fhd = False<EOL>try:<EOL><INDENT>if isinstance(fname, basestring):<EOL><INDENT>if sys.version_info[<NUM_LIT:0>] == <NUM_LIT:2>:<EOL><INDENT>fhd = iter(np.lib._datasource.open(fname, '<STR_LIT>'))<EOL><DEDENT>else:<EOL><INDENT>fhd = iter(np.lib._datasource.open(fname, '<STR_LIT:rb>'))<EOL><DEDENT>own_fhd = True<EOL><DEDENT>else:<EOL><INDENT>fhd = iter(fname)<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % type(fname))<EOL><DEDENT>split_line = LineSplitter(delimiter=delimiter, comments=comments,<EOL>autostrip=autostrip)._handyman<EOL>validate_names = NameValidator(excludelist=excludelist,<EOL>deletechars=deletechars,<EOL>case_sensitive=case_sensitive,<EOL>replace_space=replace_space)<EOL>if skiprows:<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>DeprecationWarning)<EOL>skip_header = skiprows<EOL><DEDENT>for i in range(skip_header):<EOL><INDENT>next(fhd)<EOL><DEDENT>first_values = None<EOL>try:<EOL><INDENT>while not first_values:<EOL><INDENT>first_line = next(fhd)<EOL>if names is True:<EOL><INDENT>if comments in first_line:<EOL><INDENT>first_line = (<EOL>asbytes('<STR_LIT>').join(first_line.split(comments)[<NUM_LIT:1>:]))<EOL><DEDENT><DEDENT>first_values = split_line(first_line)<EOL><DEDENT><DEDENT>except StopIteration:<EOL><INDENT>first_line = asbytes('<STR_LIT>')<EOL>first_values = []<EOL>warnings.warn('<STR_LIT>' % fname)<EOL><DEDENT>if names is True:<EOL><INDENT>fval = first_values[<NUM_LIT:0>].strip()<EOL>if fval in comments:<EOL><INDENT>del first_values[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>if usecols is not None:<EOL><INDENT>try:<EOL><INDENT>usecols = [_.strip() for _ in usecols.split("<STR_LIT:U+002C>")]<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:<EOL><INDENT>usecols = list(usecols)<EOL><DEDENT>except TypeError:<EOL><INDENT>usecols = [usecols, ]<EOL><DEDENT><DEDENT><DEDENT>nbcols = len(usecols or first_values)<EOL>if names is True:<EOL><INDENT>names = validate_names([_bytes_to_name(_.strip())<EOL>for _ in first_values])<EOL>first_line = asbytes('<STR_LIT>')<EOL><DEDENT>elif _is_string_like(names):<EOL><INDENT>names = validate_names([_.strip() for _ in names.split('<STR_LIT:U+002C>')])<EOL><DEDENT>elif names:<EOL><INDENT>names = validate_names(names)<EOL><DEDENT>if dtype is not None:<EOL><INDENT>dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names)<EOL><DEDENT>if names is not None:<EOL><INDENT>names = list(names)<EOL><DEDENT>if usecols:<EOL><INDENT>for (i, current) in enumerate(usecols):<EOL><INDENT>if _is_string_like(current):<EOL><INDENT>usecols[i] = names.index(current)<EOL><DEDENT>elif current < <NUM_LIT:0>:<EOL><INDENT>usecols[i] = current + len(first_values)<EOL><DEDENT><DEDENT>if (dtype is not None) and (len(dtype) > nbcols):<EOL><INDENT>descr = dtype.descr<EOL>dtype = np.dtype([descr[_] for _ in usecols])<EOL>names = list(dtype.names)<EOL><DEDENT>elif (names is not None) and (len(names) > nbcols):<EOL><INDENT>names = [names[_] for _ in usecols]<EOL><DEDENT><DEDENT>elif (names is not None) and (dtype is not None):<EOL><INDENT>names = list(dtype.names)<EOL><DEDENT>user_missing_values = missing_values or ()<EOL>missing_values = [list([asbytes('<STR_LIT>')]) for _ in range(nbcols)]<EOL>if isinstance(user_missing_values, dict):<EOL><INDENT>for (key, val) in user_missing_values.items():<EOL><INDENT>if _is_string_like(key):<EOL><INDENT>try:<EOL><INDENT>key = names.index(key)<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>if usecols:<EOL><INDENT>try:<EOL><INDENT>key = usecols.index(key)<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if isinstance(val, (list, tuple)):<EOL><INDENT>val = [str(_) for _ in val]<EOL><DEDENT>else:<EOL><INDENT>val = [str(val), ]<EOL><DEDENT>if key is None:<EOL><INDENT>for miss in missing_values:<EOL><INDENT>miss.extend(val)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>missing_values[key].extend(val)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(user_missing_values, (list, tuple)):<EOL><INDENT>for (value, entry) in zip(user_missing_values, missing_values):<EOL><INDENT>value = str(value)<EOL>if value not in entry:<EOL><INDENT>entry.append(value)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(user_missing_values, bytes):<EOL><INDENT>user_value = user_missing_values.split(asbytes("<STR_LIT:U+002C>"))<EOL>for entry in missing_values:<EOL><INDENT>entry.extend(user_value)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for entry in missing_values:<EOL><INDENT>entry.extend([str(user_missing_values)])<EOL><DEDENT><DEDENT>if missing != asbytes('<STR_LIT>'):<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>DeprecationWarning)<EOL>values = [str(_) for _ in missing.split(asbytes("<STR_LIT:U+002C>"))]<EOL>for entry in missing_values:<EOL><INDENT>entry.extend(values)<EOL><DEDENT><DEDENT>user_filling_values = filling_values<EOL>if user_filling_values is None:<EOL><INDENT>user_filling_values = []<EOL><DEDENT>filling_values = [None] * nbcols<EOL>if isinstance(user_filling_values, dict):<EOL><INDENT>for (key, val) in user_filling_values.items():<EOL><INDENT>if _is_string_like(key):<EOL><INDENT>try:<EOL><INDENT>key = names.index(key)<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>if usecols:<EOL><INDENT>try:<EOL><INDENT>key = usecols.index(key)<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>filling_values[key] = val<EOL><DEDENT><DEDENT>elif isinstance(user_filling_values, (list, tuple)):<EOL><INDENT>n = len(user_filling_values)<EOL>if (n <= nbcols):<EOL><INDENT>filling_values[:n] = user_filling_values<EOL><DEDENT>else:<EOL><INDENT>filling_values = user_filling_values[:nbcols]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>filling_values = [user_filling_values] * nbcols<EOL><DEDENT>if dtype is None:<EOL><INDENT>converters = [StringConverter(None, missing_values=miss, default=fill)<EOL>for (miss, fill) in zip(missing_values, filling_values)]<EOL><DEDENT>else:<EOL><INDENT>dtype_flat = flatten_dtype(dtype, flatten_base=True)<EOL>if len(dtype_flat) > <NUM_LIT:1>:<EOL><INDENT>zipit = zip(dtype_flat, missing_values, filling_values)<EOL>converters = [StringConverter(dt, locked=True,<EOL>missing_values=miss, default=fill)<EOL>for (dt, miss, fill) in zipit]<EOL><DEDENT>else:<EOL><INDENT>zipit = zip(missing_values, filling_values)<EOL>converters = [StringConverter(dtype, locked=True,<EOL>missing_values=miss, default=fill)<EOL>for (miss, fill) in zipit]<EOL><DEDENT><DEDENT>uc_update = []<EOL>for (j, conv) in user_converters.items():<EOL><INDENT>if _is_string_like(j):<EOL><INDENT>try:<EOL><INDENT>j = names.index(j)<EOL>i = j<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>elif usecols:<EOL><INDENT>try:<EOL><INDENT>i = usecols.index(j)<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>else:<EOL><INDENT>i = j<EOL><DEDENT>if len(first_line):<EOL><INDENT>testing_value = first_values[j]<EOL><DEDENT>else:<EOL><INDENT>testing_value = None<EOL><DEDENT>converters[i].update(conv, locked=True,<EOL>testing_value=testing_value,<EOL>default=filling_values[i],<EOL>missing_values=missing_values[i],)<EOL>uc_update.append((i, conv))<EOL><DEDENT>user_converters.update(uc_update)<EOL>rows = []<EOL>append_to_rows = rows.append<EOL>if usemask:<EOL><INDENT>masks = []<EOL>append_to_masks = masks.append<EOL><DEDENT>invalid = []<EOL>append_to_invalid = invalid.append<EOL>for (i, line) in enumerate(itertools.chain([first_line, ], fhd)):<EOL><INDENT>values = split_line(line)<EOL>nbvalues = len(values)<EOL>if nbvalues == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>if usecols:<EOL><INDENT>try:<EOL><INDENT>values = [values[_] for _ in usecols]<EOL><DEDENT>except IndexError:<EOL><INDENT>append_to_invalid((i + skip_header + <NUM_LIT:1>, nbvalues))<EOL>continue<EOL><DEDENT><DEDENT>elif nbvalues != nbcols:<EOL><INDENT>append_to_invalid((i + skip_header + <NUM_LIT:1>, nbvalues))<EOL>continue<EOL><DEDENT>append_to_rows(tuple(values))<EOL>if usemask:<EOL><INDENT>append_to_masks(tuple([v.strip() in m<EOL>for (v, m) in zip(values, missing_values)]))<EOL><DEDENT><DEDENT>if own_fhd:<EOL><INDENT>fhd.close()<EOL><DEDENT>if dtype is None:<EOL><INDENT>for (i, converter) in enumerate(converters):<EOL><INDENT>current_column = [itemgetter(i)(_m) for _m in rows]<EOL>try:<EOL><INDENT>converter.iterupgrade(current_column)<EOL><DEDENT>except ConverterLockError:<EOL><INDENT>errmsg = "<STR_LIT>" % i<EOL>current_column = map(itemgetter(i), rows)<EOL>for (j, value) in enumerate(current_column):<EOL><INDENT>try:<EOL><INDENT>converter.upgrade(value)<EOL><DEDENT>except (ConverterError, ValueError):<EOL><INDENT>errmsg += "<STR_LIT>"<EOL>errmsg %= (j + <NUM_LIT:1> + skip_header, value)<EOL>raise ConverterError(errmsg)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>nbinvalid = len(invalid)<EOL>if nbinvalid > <NUM_LIT:0>:<EOL><INDENT>nbrows = len(rows) + nbinvalid - skip_footer<EOL>template = "<STR_LIT>" % nbcols<EOL>if skip_footer > <NUM_LIT:0>:<EOL><INDENT>nbinvalid_skipped = len([_ for _ in invalid<EOL>if _[<NUM_LIT:0>] > nbrows + skip_header])<EOL>invalid = invalid[:nbinvalid - nbinvalid_skipped]<EOL>skip_footer -= nbinvalid_skipped<EOL><INDENT>nbrows -= skip_footer<EOL>errmsg = [template % (i, nb)<EOL>for (i, nb) in invalid if i < nbrows]<EOL>
Load data from a text file, with missing values handled as specified.

Each line past the first `skip_header` lines is split at the `delimiter`
character, and characters following the `comments` character are discarded.

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 must return byte strings in Python 3k.
dtype : dtype, optional
    Data type of the resulting array.
    If None, the dtypes will be determined by the contents of each
    column, individually.
comments : str, optional
    The character used to indicate the start of a comment.
    All the characters occurring on a line after a comment are discarded
delimiter : str, int, or sequence, optional
    The string used to separate values.  By default, any consecutive
    whitespaces act as delimiter.  An integer or sequence of integers
    can also be provided as width(s) of each field.
skip_rows : int, optional
    `skip_rows` was deprecated in numpy 1.5, and will be removed in
    numpy 2.0. Please use `skip_header` instead.
skip_header : int, optional
    The number of lines to skip at the beginning of the file.
skip_footer : int, optional
    The number of lines to skip at the end of the file.
converters : variable, optional
    The set of functions that convert the data of a column to a value.
    The converters can also be used to provide a default value
    for missing data: ``converters = {3: lambda s: float(s or 0)}``.
missing : variable, optional
    `missing` was deprecated in numpy 1.5, and will be removed in
    numpy 2.0. Please use `missing_values` instead.
missing_values : variable, optional
    The set of strings corresponding to missing data.
filling_values : variable, optional
    The set of values to be used as default when the data are missing.
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.
names : {None, True, str, sequence}, optional
    If `names` is True, the field names are read from the first valid line
    after the first `skip_header` lines.
    If `names` is a sequence or a single-string of comma-separated names,
    the names will be used to define the field names in a structured dtype.
    If `names` is None, the names of the dtype fields will be used, if any.
excludelist : sequence, optional
    A list of names to exclude. This list is appended to the default list
    ['return','file','print']. Excluded names are appended an underscore:
    for example, `file` would become `file_`.
deletechars : str, optional
    A string combining invalid characters that must be deleted from the
    names.
defaultfmt : str, optional
    A format used to define default field names, such as "f%i" or "f_%02i".
autostrip : bool, optional
    Whether to automatically strip white spaces from the variables.
replace_space : char, optional
    Character(s) used in replacement of white spaces in the variables
    names. By default, use a '_'.
case_sensitive : {True, False, 'upper', 'lower'}, optional
    If True, field names are case sensitive.
    If False or 'upper', field names are converted to upper case.
    If 'lower', field names are converted to lower case.
unpack : bool, optional
    If True, the returned array is transposed, so that arguments may be
    unpacked using ``x, y, z = loadtxt(...)``
usemask : bool, optional
    If True, return a masked array.
    If False, return a regular array.
loose : bool, optional
    If True, do not raise errors for invalid values.
invalid_raise : bool, optional
    If True, an exception is raised if an inconsistency is detected in the
    number of columns.
    If False, a warning is emitted and the offending lines are skipped.

Returns
-------
out : ndarray
    Data read from the text file. If `usemask` is True, this is a
    masked array.

See Also
--------
numpy.loadtxt : equivalent function when no data is missing.

Notes
-----
* When spaces are used as delimiters, or when no delimiter has been given
  as input, there should not be any missing data between two fields.
* When the variables are named (either by a flexible dtype or with `names`,
  there must not be any header in the file (else a ValueError
  exception is raised).
* Individual values are not stripped of spaces by default.
  When using a custom converter, make sure the function does remove spaces.

References
----------
.. [1] Numpy User Guide, section `I/O with Numpy
       <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_.

Examples
---------
>>> from StringIO import StringIO
>>> import numpy as np

Comma delimited file with mixed dtype

>>> s = StringIO("1,1.3,abcde")
>>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
... ('mystring','S5')], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
      dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])

Using dtype = None

>>> s.seek(0) # needed for StringIO example only
>>> data = np.genfromtxt(s, dtype=None,
... names = ['myint','myfloat','mystring'], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
      dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])

Specifying dtype and names

>>> s.seek(0)
>>> data = np.genfromtxt(s, dtype="i8,f8,S5",
... names=['myint','myfloat','mystring'], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
      dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])

An example with fixed-width columns

>>> s = StringIO("11.3abcde")
>>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],
...     delimiter=[1,3,5])
>>> data
array((1, 1.3, 'abcde'),
      dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')])