NumPy | loadtxt method
Start your free 7-days trial now!
NumPy's loadtxt(~) method reads a text file, and parses its content into a NumPy array.
Parameters
1. fnamelink | string
The name of the file. If the file is not in the same directory as the script, make sure to include the path to the file as well.
2. dtypelink | string or type | optional
The desired data-type of the constructed array. By default, dtype=float.
3. commentslink | string or list of strings | optional
If your input file contains comments, then you can specify what identifies a comment. By default, comments="#", that is, characters after the # in the same line will be treated as a comment. You can set None if your text file does not include any comment.
4. delimiterlink | string | optional
The string used to separate your data. By default, the delimiter is a whitespace.
5. converterslink | dict<int,function> | optional
You can apply a mapping to transform your column values. The key is the integer index of the column, and the value is the desired mapping. Check examples below for clarification. By default, dict=None.
6. skiprowslink | int | optional
The number of rows in the beginning to skip. Note that this includes comments. By default, skiprows=0.
7. usecolslink | int or sequence | optional
The integer indices of the columns you want to read. By default, usecols=None, that is, all columns are read.
8. unpacklink | boolean | optional
Instead of having one giant Numpy array, you could retrieve column arrays individually by setting unpack=True. For instance, col_one, col_two = np.loadtxt(~, unpack=True). By default, unpack=False.
9. ndminlink | int | optional
The minimum number of dimensions you want. The allowed values are 0, 1 and 2.
10. encoding | string | optional
The encoding to use when reading the file (e.g. "latin-1", "iso-8859-1"). By default, encoding="bytes".
11. max_rowslink | int | optional
The maximum number of rows to read. By default, all lines are read.
Return value
A NumPy array with the imported data.
Examples
Basic usage
Suppose we have the following text-file called sample.txt:
1 2 3 45 6 7 8
To import this file:
a = np.loadtxt("sample.txt")a
array([[1., 2., 3., 4.], [5., 6., 7., 8.]])
Note that this Python script resides in the same directory as sample.txt.
Also, notice how the default data type chosen by Numpy is float64, regardless of whether or not the numbers in the text file are all integers:
float64
Specifying the desired data type
Instead of using the default float64, we can specify a type using dtype:
a = np.loadtxt("sample.txt", dtype=int)a
array([[1, 2, 3, 4], [5, 6, 7, 8]])
Handling comments
Suppose our sample.txt file is as follows:
1 2 3 4 # I'm the first row!5 6 7 8 // I'm the second row!
To strip out comments in the text-file, specify comments:
a = np.loadtxt("sample.txt", comments=["#", "//"])a
array([[1., 2., 3., 4.], [5., 6., 7., 8.]])
Specifying a custom delimiter
Suppose our sample.txt file is as follows:
1,23,4
To use a comma as the delimiter:
a = np.loadtxt("sample.txt", delimiter=",")a
1,23,4
Specifying converters
Suppose our sample.txt file is as follows:
1 23 4
Just as an arbitrary example, suppose we wanted to add 10 to all values of the 1st column, and make all the values of the 2nd column be 20:
a = np.loadtxt("sample.txt", converters={0: lambda x: int(x) + 10, 1: lambda x: 20})a
array([[11., 20.], [13., 20.]])
Skipping rows
Suppose our sample.txt file is as follows:
1 2 34 5 67 8 9
To skip the first row:
a = np.loadtxt("sample.txt", skiprows=1)a
array([[4., 5., 6.], [7., 8., 9.]])
Reading only certain columns
Suppose our sample.txt file is as follows:
1 2 34 5 6
To read only the 1st and 3rd columns (i.e. column index 0 and 2):
a = np.loadtxt("sample.txt", usecols=[0,2])a
array([[1., 3.], [4., 6.]])
Unpacking columns
Suppose our sample.txt file is as follows:
1 23 4
To retrieve the data per column instead of a single NumPy array:
Specifying the desired dimension
Suppose our sample.txt only had one row:
1 2 3 4
By default, loadtxt(~) will generate an one-dimensional array:
a = np.loadtxt("sample.txt")a
array([1., 2., 3., 4.])
We can specify that we want our array to be two-dimensional by:
a = np.loadtxt("sample.txt", ndmin=2)a
array([[1., 2., 3., 4.]])
Specifying a maximum number of rows to read
Suppose our sample.txt file is as follows:
1 23 45 6
To read only the first two rows:
a = np.loadtxt("sample.txt", max_rows=2)a