bundles / numpy 2.4.4 / numpy / genfromtxt
_ArrayFunctionDispatcher
numpy:genfromtxt
source: /numpy/lib/_npyio_impl.py :1735
Signature
def genfromtxt ( fname , dtype = <class 'float'> , comments = # , delimiter = None , skip_header = 0 , skip_footer = 0 , converters = None , missing_values = None , filling_values = None , usecols = None , names = None , excludelist = None , deletechars = !#$%&'()*+,-./:;<=>?@[\]^{|}~ , replace_space = _ , autostrip = False , case_sensitive = True , defaultfmt = f%i , unpack = None , usemask = False , loose = True , invalid_raise = True , max_rows = None , encoding = None , * , ndmin = 0 , like = None ) Summary
Load data from a text file, with missing values handled as specified.
Extended Summary
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, str, pathlib.Path, list of str, generatorFile, filename, list, or generator to read. If the filename extension is
.gzor.bz2, the file is first decompressed. Note that generators must return bytes or strings. The strings in a list or produced by a generator are treated as lines.dtype: dtype, optionalData type of the resulting array. If None, the dtypes will be determined by the contents of each column, individually.
comments: str, optionalThe 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, optionalThe 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.
skiprows: int, optionalskiprows was removed in numpy 1.10. Please use
skip_headerinstead.skip_header: int, optionalThe number of lines to skip at the beginning of the file.
skip_footer: int, optionalThe number of lines to skip at the end of the file.
converters: variable, optionalThe 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, optionalmissing was removed in numpy 1.10. Please use
missing_valuesinstead.missing_values: variable, optionalThe set of strings corresponding to missing data.
filling_values: variable, optionalThe set of values to be used as default when the data are missing.
usecols: sequence, optionalWhich 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}, optionalIf
namesis True, the field names are read from the first line after the firstskip_headerlines. This line can optionally be preceded by a comment delimiter. Any content before the comment delimiter is discarded. Ifnamesis a sequence or a single-string of comma-separated names, the names will be used to define the field names in a structured dtype. Ifnamesis None, the names of the dtype fields will be used, if any.excludelist: sequence, optionalA list of names to exclude. This list is appended to the default list ['return','file','print']. Excluded names are appended with an underscore: for example,
filewould becomefile_.deletechars: str, optionalA string combining invalid characters that must be deleted from the names.
defaultfmt: str, optionalA format used to define default field names, such as "f%i" or "f_%02i".
autostrip: bool, optionalWhether to automatically strip white spaces from the variables.
replace_space: char, optionalCharacter(s) used in replacement of white spaces in the variable names. By default, use a '_'.
case_sensitive: {True, False, 'upper', 'lower'}, optionalIf 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, optionalIf True, the returned array is transposed, so that arguments may be unpacked using
x, y, z = genfromtxt(...). When used with a structured data-type, arrays are returned for each field. Default is False.usemask: bool, optionalIf True, return a masked array. If False, return a regular array.
loose: bool, optionalIf True, do not raise errors for invalid values.
invalid_raise: bool, optionalIf 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.
max_rows: int, optionalThe maximum number of rows to read. Must not be used with skip_footer at the same time. If given, the value must be at least 1. Default is to read the entire file.
encoding: str, optionalEncoding used to decode the inputfile. Does not apply when
fnameis a file object. The special value 'bytes' enables backward compatibility workarounds that ensure that you receive byte arrays when possible and passes latin1 encoded strings to converters. Override this value to receive unicode arrays and pass strings as input to converters. If set to None the system default is used. The default value is 'bytes'.ndmin: int, optionalSame parameter as
loadtxtlike: array_like, optionalReference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as
likesupports the__array_function__protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
Returns
out: ndarrayData read from the text file. If
usemaskis True, this is a masked array.
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 variables are named (either by a flexible dtype or with a
namessequence), 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.
Custom converters may receive unexpected values due to dtype discovery.
Examples
from io import StringIO import numpy as np✓
s = StringIO("1,1.3,abcde") data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ('mystring','S5')], delimiter=",") data✓
_ = s.seek(0) # needed for StringIO example only data = np.genfromtxt(s, dtype=None, names = ['myint','myfloat','mystring'], delimiter=",") data✓
_ = s.seek(0) data = np.genfromtxt(s, dtype="i8,f8,S5", names=['myint','myfloat','mystring'], delimiter=",") data✓
s = StringIO("11.3abcde") data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], delimiter=[1,3,5]) data✓
f = StringIO(''' text,# of chars hello world,11 numpy,5''')✓
np.genfromtxt(f, dtype='S12,S12', delimiter=',')
✗See also
- numpy.loadtxt
equivalent function when no data is missing.
Aliases
-
numpy.genfromtxt -
numpy.lib._npyio_impl._genfromtxt_with_like
Referenced by
This package
- release:1.16.0-notes
- release:1.20.0-notes
- user:basics.creation
- user:basics.io.genfromtxt
- user:how-to-io
- release:1.16.0-notes
- release:1.20.0-notes
- user:basics.creation
- user:basics.io.genfromtxt
- user:how-to-io
- release:1.16.0-notes
- release:1.20.0-notes
- user:basics.creation
- user:basics.io.genfromtxt
- user:how-to-io