Importing data from text files into NumPy arrays is a common task in data analysis and scientific computing. NumPy provides useful functions that allow you to efficiently read structured text data and convert it into arrays for fast numerical operations. Among these loadtxt() and genfromtxt() are the most widely used methods.
NumPy loadtxt() Method
numpy.loadtxt() is a fast and efficient way to load numerical or structured data from text files into NumPy arrays. It works best with clean, consistently formatted datasets such as CSV, TSV or space-separated files. Compared to manual file handling it enables reading entire files in a single line of code.
Syntax: numpy.loadtxt(fname, delimiter=None, dtype=float, skiprows=0, usecols=None, comments='#', max_rows=None)
- fname: File name or path
- delimiter: Character separating values
- dtype: Data type of the resulting array
- skiprows: Number of initial rows to skip
- usecols: Specific columns to read
- comments: Character indicating comment lines
- max_rows: Maximum number of rows to read
1. Importing a Simple Text File into a NumPy Array
numpy.loadtxt() reads data from a text file and stores it directly in a NumPy array. By specifying dtype=int, all values are converted into integers, making the approach concise, efficient, and suitable for clean numerical datasets.

import numpy as np
data = np.loadtxt(
"example.txt",
dtype=int
)
print("Loaded Data:")
print(data)
Output:
Loaded Data:
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]]
2 Importing a CSV File Using a Delimiter
When working with CSV files values are separated by commas instead of spaces. The delimiter parameter in numpy.loadtxt() allows NumPy to correctly interpret and split each value making it easy to load CSV data directly into an array.

import numpy as np
data = np.loadtxt(
"csv1.txt",
delimiter=","
)
print("CSV Data:")
print(data)
Output:
CSV Data:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
3. Handling Comment Lines in Text Files
Some text or CSV files include comment lines or metadata that should not be treated as data. The comments parameter in numpy.loadtxt() allows you to skip such lines while loading the numerical data into a NumPy array.

import numpy as np
data = np.loadtxt(
"csv_comment.txt",
delimiter=",",
comments="#"
)
print("Data without comments:")
print(data)
Output:
Data without comments:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
4. Skipping Initial Rows
Some text or CSV files include metadata at the top that should not be treated as data. The skiprows parameter in numpy.loadtxt() allows you to skip these rows and start reading from the actual data.

import numpy as np
data = np.loadtxt(
"skip_rows.txt",
delimiter=",",
skiprows=3
)
print("Data after skipping metadata:")
print(data)
Output:
Data after skipping metadata:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
5. Importing Only One Column from a Text File
When working with structured data you may only need specific columns. The usecols parameter in numpy.loadtxt() allows you to select one or more columns while skipping headers with skiprows.

import numpy as np
names = np.loadtxt(
"one_col.txt",
usecols=1,
skiprows=1,
dtype=str
)
for name in names:
print(name)
Output:
Ankit
Bunty
Tinku
Rina
Rajesh
numpy.genfromtxt() Method
numpy.genfromtxt() is a flexible alternative to loadtxt(), capable of handling missing values, mixed data types, and irregular file formats. It’s ideal for real-world datasets that may be incomplete or inconsistent.
1. Importing a Text File Using np.genfromtxt()
The numpy.genfromtxt() function can read structured text files while handling different data types and encodings, making it ideal for textual or categorical data.

import numpy as np
data = np.genfromtxt(
"example1.txt",
dtype=str,
delimiter=",",
encoding="utf-8"
)
print(data)
Output:
[['a' 'b' 'c' 'd']
['e' 'f' 'g' 'h']]
2. Skipping the Last Row of a Text File
numpy.genfromtxt() allows skipping lines at the end of a file using the skip_footer parameter, which is useful when files contain summaries, footers, or extra notes.

import numpy as np
data = np.genfromtxt(
"skip_last.txt",
dtype=str,
skip_footer=1
)
print(data)
Output:
[['This' 'is' 'GeeksForGeeks' 'Website']
['How' 'are' 'You' 'Geeks?']
['Geeks' 'for' 'Geeks' 'GFG']]
3. Handling Missing Values in a Text File
numpy.genfromtxt() can automatically handle missing values in datasets replacing them with NaN to allow safe numerical processing. This makes it more robust than loadtxt() for real-world, incomplete data.

import numpy as np
data = np.genfromtxt(
"missing_txt.txt",
delimiter=",",
dtype=float
)
print(data)
Output:
[[ 1. 2. 3.]
[ 4. nan 6.]
[ 7. 8. 9.]]
4. Replacing Missing Values with Custom Defaults
numpy.genfromtxt() lets you replace missing values with a custom default using the filling_values parameter. This is useful when missing data could affect calculations or modeling.
import numpy as np
data = np.genfromtxt(
"missing_txt.txt",
delimiter=",",
dtype=float,
filling_values=0
)
print(data)