numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None, *, like=None)

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

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.

versionadded

The strings produced by the Python float.hex method can be used as input for floats.

Parameters

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, 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 string used to separate values. For backwards compatibility, byte strings will be decoded as 'latin1'. The default is whitespace.

converters : dict, optional

A dictionary mapping column number to a function that will parse the column string into the desired value. 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 :None:None:`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.

versionchanged

When a single column has to be read it is possible to use an integer instead of a tuple. E.g usecols = 3 reads the fourth column the same way as usecols = (3,) would.

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 :None:None:`ndmin` dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2.

versionadded
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 'bytes'.

versionadded
max_rows : int, optional

Read :None:None:`max_rows` lines of content after :None:None:`skiprows` lines. The default is to read all the lines.

versionadded
like : array_like

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.

versionadded

Returns

out : ndarray

Data read from the text file.

Load data from a text file.

See Also

fromregex
fromstring
genfromtxt

Load data with missing values handled as specified.

load
scipy.io.loadmat

reads MATLAB data files

Examples

>>> from io 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([(b'M', 21, 72.), (b'F', 35, 58.)], 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.])

This example shows how :None:None:`converters` can be used to convert a field with a trailing minus sign into a negative number.

>>> s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94')
... def conv(fld):
...  return -float(fld[:-1]) if fld.endswith(b'-') else float(fld) ...
>>> np.loadtxt(s, converters={0: conv, 1: conv})
array([[ 10.01, -31.25],
       [ 19.22,  64.31],
       [-17.57,  63.94]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.fromfile numpy.genfromtxt numpy.savetxt numpy.load numpy.fromregex

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/lib/npyio.py#829
type: <class 'function'>
Commit: