numpy.loadtxt — NumPy v1.26 Manual (2024)

numpy.loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None, *, quotechar=None, like=None)[source]#

Load data from a text file.

Parameters:
fnamefile, str, pathlib.Path, list of str, generator

File, filename, list, or generator to read. If the filenameextension is .gz or .bz2, the file is first decompressed. Notethat generators must return bytes or strings. The stringsin a list or produced by a generator are treated as lines.

dtypedata-type, optional

Data-type of the resulting array; default: float. If this is astructured data-type, the resulting array will be 1-dimensional, andeach row will be interpreted as an element of the array. In thiscase, the number of columns used must match the number of fields inthe data-type.

commentsstr or sequence of str or None, optional

The characters or list of characters used to indicate the start of acomment. None implies no comments. For backwards compatibility, bytestrings will be decoded as ‘latin1’. The default is ‘#’.

delimiterstr, 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 characterscannot be used as the delimiter.

convertersdict or callable, optional

Converter functions to customize value parsing. If converters iscallable, the function is applied to all columns, else it must be adict 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 columnswas added.

skiprowsint, optional

Skip the first skiprows lines, including comments; default: 0.

usecolsint 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.

Changed in version 1.11.0: When a single column has to be read it is possible to usean integer instead of a tuple. E.g usecols = 3 reads thefourth column the same way as usecols = (3,) would.

unpackbool, optional

If True, the returned array is transposed, so that arguments may beunpacked using x, y, z = loadtxt(...). When used with astructured data-type, arrays are returned for each field.Default is False.

ndminint, optional

The returned array will have at least ndmin dimensions.Otherwise mono-dimensional axes will be squeezed.Legal values: 0 (default), 1 or 2.

New in version 1.6.0.

encodingstr, optional

Encoding used to decode the inputfile. Does not apply to input streams.The special value ‘bytes’ enables backward compatibility workaroundsthat ensures you receive byte arrays as results if possible and passes‘latin1’ encoded strings to converters. Override this value to receiveunicode arrays and pass strings as input to converters. If set to Nonethe system default is used. The default value is ‘bytes’.

New in version 1.14.0.

max_rowsint, optional

Read max_rows rows of content after skiprows lines. The default isto read all the rows. Note that empty rows containing no data such asempty lines and comment lines are not counted towards max_rows,while such lines are counted in skiprows.

New in version 1.16.0.

Changed in version 1.23.0: Lines containing no data, including comment lines (e.g., linesstarting with ‘#’ or as specified via comments) are not countedtowards max_rows.

quotecharunicode 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 withina quoted item. The default value is quotechar=None, which meansquoting support is disabled.

If two consecutive instances of quotechar are found within a quotedfield, the first is treated as an escape character. See examples.

New in version 1.23.0.

likearray_like, optional

Reference object to allow the creation of arrays which are notNumPy arrays. If an array-like passed in as like supportsthe __array_function__ protocol, the result will be definedby it. In this case, it ensures the creation of an array objectcompatible with that passed in via this argument.

New in version 1.20.0.

Returns:
outndarray

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. Thegenfromtxt 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 beable to read all values. If all rows do not have same number of values, asubset of up to n columns (where n is the least number of values presentin all rows) can be read by specifying the columns via usecols.

New in version 1.10.0.

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

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)>>> xarray([1., 3.])>>> yarray([2., 4.])

The converters argument is used to specify functions to preprocess thetext prior to parsing. converters can be a dictionary that mapspreprocessing functions to each column:

>>> s = StringIO("1.618, 2.296\n3.141, 4.669\n")>>> conv = {...  0: lambda x: np.floor(float(x)), # conversion fn for column 0...  1: lambda x: np.ceil(float(x)), # conversion fn for column 1... }>>> np.loadtxt(s, delimiter=",", converters=conv)array([[1., 3.], [3., 5.]])

converters can be a callable instead of a dictionary, in which case itis applied to all columns:

>>> s = StringIO("0xDE 0xAD\n0xC0 0xDE")>>> import functools>>> conv = functools.partial(int, base=16)>>> np.loadtxt(s, converters=conv)array([[222., 173.], [192., 222.]])

This example shows how converters can be used to convert a fieldwith 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=conv)array([[ 10.01, -31.25], [ 19.22, 64.31], [-17.57, 63.94]])

Using a callable as the converter can be particularly useful for handlingvalues with different formatting, e.g. floats with underscores:

>>> s = StringIO("1 2.7 100_000")>>> np.loadtxt(s, converters=float)array([1.e+00, 2.7e+00, 1.e+05])

This idea can be extended to automatically handle values specified inmany different formats:

>>> def conv(val):...  try:...  return float(val)...  except ValueError:...  return float.fromhex(val)>>> s = StringIO("1, 2.5, 3_000, 0b4, 0x1.4000000000000p+2")>>> np.loadtxt(s, delimiter=",", converters=conv, encoding=None)array([1.0e+00, 2.5e+00, 3.0e+03, 1.8e+02, 5.0e+00])

Note that with the default encoding="bytes", the inputs to theconverter function are latin-1 encoded byte strings. To deactivate theimplicit encoding prior to conversion, use encoding=None

>>> s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94')>>> conv = lambda x: -float(x[:-1]) if x.endswith('-') else float(x)>>> np.loadtxt(s, converters=conv, encoding=None)array([[ 10.01, -31.25], [ 19.22, 64.31], [-17.57, 63.94]])

Support for quoted fields is enabled with the quotechar parameter.Comment and delimiter characters are ignored when they appear within aquoted item delineated by quotechar:

>>> s = StringIO('"alpha, #42", 10.0\n"beta, #64", 2.0\n')>>> dtype = np.dtype([("label", "U12"), ("value", float)])>>> np.loadtxt(s, dtype=dtype, delimiter=",", quotechar='"')array([('alpha, #42', 10.), ('beta, #64', 2.)], dtype=[('label', '<U12'), ('value', '<f8')])

Quoted fields can be separated by multiple whitespace characters:

>>> s = StringIO('"alpha, #42" 10.0\n"beta, #64" 2.0\n')>>> dtype = np.dtype([("label", "U12"), ("value", float)])>>> np.loadtxt(s, dtype=dtype, delimiter=None, quotechar='"')array([('alpha, #42', 10.), ('beta, #64', 2.)], dtype=[('label', '<U12'), ('value', '<f8')])

Two consecutive quote characters within a quoted field are treated as asingle escaped character:

>>> s = StringIO('"Hello, my name is ""Monty""!"')>>> np.loadtxt(s, dtype="U", delimiter=",", quotechar='"')array('Hello, my name is "Monty"!', dtype='<U26')

Read subset of columns when all rows do not contain equal number of values:

>>> d = StringIO("1 2\n2 4\n3 9 12\n4 16 20")>>> np.loadtxt(d, usecols=(0, 1))array([[ 1., 2.], [ 2., 4.], [ 3., 9.], [ 4., 16.]])
numpy.loadtxt — NumPy v1.26 Manual (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6267

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.