nlcpy.loadtxt
- nlcpy.loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None)[source]
Loads data from a text file.
Each row in the text file must have the same number of values.
- Parameters
- fnamefile or str or pathlib.Path
File, filename, or generator to read. If the filename extension is
.gz
or.bz2
, the file is first decompressed. Note that generators should return byte strings for Python 3k.- dtypedtype, optional
Data-type of the resulting array; default: float.
- commentsstr 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 ‘#’.
- delimiterstr, optional
The string used to separate values. For backwards compatibility, byte strings will be decoded as ‘latin1’. The default is whitespace.
- convertersdict, 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:converters = {3: lambda s: float(s.strip() or 0)}
. Default: None.- 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.- unpackbool, optional
If True, the returned array is transposed, so that arguments may be unpacked using
x, y, z = loadtxt(...)
. 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.
- encodingstr, 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’.
- max_rowsint, optional
Read max_rows lines of content after skiprows lines. The default is to read all the lines.
- Returns
- outndarray
Data read from the text file.
See also
load
Loads arrays or pickled objects from
.npy
,.npz
or pickled files.
Note
This function aims to be a fast reader for simply formatted files. The strings produced by the Python float.hex method can be used as input for floats.
Examples
>>> import nlcpy as vp >>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO(u"0 1\n2 3") >>> vp.loadtxt(c) array([[0., 1.], [2., 3.]]) >>> c = StringIO(u"1,0,2\n3,0,4") >>> x, y = vp.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([1., 3.]) >>> y array([2., 4.])