fname: file, str, pathlib.Path, list of str, generator
File, filename, list, or generator to read. If the filename extension is .gz or .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: data-type, optional
Data-type of the resulting array; default: float. If this is a structured 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 or sequence of str or None, optional
The characters or list of characters used to indicate the start of a comment. None implies no comments. For backwards compatibility, byte strings will be decoded as ‘latin1’. The default is ‘#’.
delimiter: str, optional
The character used to separate the values. For backwards compatibility, byte strings will be decoded as ‘latin1’. The default is whitespace.
Changed in version 1.23.0: Only single character delimiters are supported. Newline characters cannot be used as the delimiter.
converters: dict or callable, optional
Converter functions to customize value parsing. If converters is callable, the function is applied to all columns, else it must be a dict that maps column number to a parser function. See examples for further details. Default: None.
Changed in version 1.23.0: The ability to pass a single callable to be applied to all columns was added.
skiprows: int, optional
Skip the first skiprows lines, including comments; default: 0.
usecols: int or 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 structured 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.
encoding: str, optional
Encoding used to decode the inputfile. Does not apply to input streams. The special value ‘bytes’ enables backward compatibility workarounds that ensures you receive byte arrays as results if 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 None.
Changed in version 2.0: Before NumPy 2, the default was 'bytes' for Python 2 compatibility. The default is now None.
max_rows: int, optional
Read max_rows rows of content after skiprows lines. The default is to read all the rows. Note that empty rows containing no data such as empty lines and comment lines are not counted towards max_rows, while such lines are counted in skiprows.
Changed in version 1.23.0: Lines containing no data, including comment lines (e.g., lines starting with ‘#’ or as specified via comments) are not counted towards max_rows.
quotechar: unicode character or None, optional
The character used to denote the start and end of a quoted item. Occurrences of the delimiter or comment characters are ignored within a quoted item. The default value is quotechar=None, which means quoting support is disabled.
If two consecutive instances of quotechar are found within a quoted field, the first is treated as an escape character. See examples.
New in version 1.23.0.
like: array_like, optional
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports 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.
New in version 1.20.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.
Each row in the input text file must have the same number of values to be able to read all values. If all rows do not have same number of values, a subset of up to n columns (where n is the least number of values present in all rows) can be read by specifying the columns via usecols.
The strings produced by the Python float.hex method can be used as input for floats.
Examples:
>>> import numpy as np>>> from io import StringIO # StringIO behaves like a file object>>> c = StringIO("0 1\n2 3")>>> np.loadtxt(c)array([[0., 1.], [2., 3.]])
>>> c = StringIO("1,0,2\n3,0,4")>>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)>>> xarray([1., 3.])>>> yarray([2., 4.])
The converters argument is used to specify functions to preprocess the text prior to parsing. converters can be a dictionary that maps preprocessing functions to each column:
Support for quoted fields is enabled with the quotechar parameter. Comment and delimiter characters are ignored when they appear within a quoted item delineated by quotechar:
Two consecutive quote characters within a quoted field are treated as a single escaped character:
>>> s = StringIO('"Hello, my name is ""Monty""!"')>>> np.loadtxt(s, dtype=np.str_, delimiter=",", quotechar='"')array('Hello, my name is "Monty"!', dtype='<U26')
Read subset of columns when all rows do not contain equal number of values: