Importing and Exporting Text Files
Learn how to load numerical data from text files and save NumPy arrays for future use. Explore np.loadtxt(), np.savetxt(), delimiter settings, headers, comments, and practical examples of reading and writing numerical datasets.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand text-based data storage.
- Load numerical data using np.loadtxt().
- Save NumPy arrays using np.savetxt().
- Work with spaces, commas, and other delimiters.
- Handle header rows and comment lines.
- Control numeric output formatting.
- Understand common file path and data-format errors.
- Apply text file input and output to practical datasets.
2. Introduction to Text File Input and Output
Text files store information as readable characters. Numerical datasets are often saved in text formats such as TXT and delimiter-separated files such as CSV.
NumPy provides functions for reading and writing numerical data. The most commonly used functions in this lesson are np.loadtxt() and np.savetxt().
| Function | Purpose |
|---|---|
np.loadtxt() |
Reads numerical data from a text file. |
np.savetxt() |
Writes numerical array data to a text file. |
NumPy supports reading numerical text data and exporting array values.
3. Creating a Sample Text File
Before loading data, create a text file containing numerical
values. For this example, create a file named
numbers.txt with the following contents.
Each line contains three values separated by spaces. NumPy can read this format directly because whitespace is the default delimiter for np.loadtxt().
4. Loading Data Using np.loadtxt()
The np.loadtxt() function reads numerical values from a text file and returns a NumPy array.
Output:
The text file is converted into a two-dimensional NumPy array. By default, np.loadtxt() typically loads numerical values as floating-point numbers.
Loading Values as Integers
Output:
The exact integer dtype may depend on your platform. The dtype=int argument requests integer values.
5. Loading Comma-Separated Data
Numerical text files sometimes use commas instead of spaces. In that case, provide the delimiter argument to np.loadtxt().
Create a file named sales.csv with these contents:
Reading CSV-Style Numerical Data
Output:
The delimiter tells NumPy how values are separated in the file. If a comma-separated file is read without the correct delimiter, loading may fail because a line can be interpreted as one invalid numerical field.
delimiter=","
for comma-separated numerical data and the default whitespace
handling for space-separated data.
6. Handling Headers and Comment Lines
Text files may contain a header row describing the columns. If the header contains text, skip it when loading purely numerical data.
Example file: student_marks.csv
Skipping the Header
Output:
The skiprows=1 argument tells NumPy to skip the first line before reading the numerical values.
Handling Comment Lines
NumPy can ignore comment lines when the file uses a comment marker, such as #.
Output:
7. Saving Arrays Using np.savetxt()
The np.savetxt() function writes numerical array values to a text file. This is useful when you want to store calculated results for later use.
Output:
The file output.txt contains the array values.
By default, np.savetxt() writes numeric values in a
text-based format.
Reading the Saved File Again
Output:
You can read the exported data again with np.loadtxt(), provided the file contains compatible numerical data.
8. Saving Data with a Specific Delimiter
The delimiter argument in np.savetxt() controls how values are separated in the output file.
Output:
The generated output.csv file contains:
The delimiter argument sets commas between values, while fmt="%d" writes values using integer formatting.
9. Formatting Values with np.savetxt()
The fmt parameter controls how values are represented in the output file. It is especially useful when exporting decimal values or when a specific number of decimal places is required.
The file contains:
The format string %.2f writes each floating-point value with two digits after the decimal point.
| Format | Meaning | Example |
|---|---|---|
%.2f |
Two decimal places | 12.35 |
%.3f |
Three decimal places | 12.346 |
%d |
Integer formatting | 12 |
%.4e |
Scientific notation with four decimal places | 1.2346e+01 |
10. Saving Headers and Comments
np.savetxt() supports header and comments arguments. These allow you to label exported columns and add explanatory information to the beginning of a file.
The generated file contains:
Here, comments="" prevents NumPy from adding the default comment prefix before the header.
Loading the Exported File
Output:
11. Working with One-Dimensional Arrays
Text files can also store one-dimensional arrays. Each element can be written on a separate line or separated by a delimiter.
Output:
A single-column text file is loaded as a one-dimensional array by default when it contains multiple numerical rows.
Preserving a Two-Dimensional Column Shape
Output:
The ndmin argument specifies a minimum number of dimensions. In this example, the values are returned as a two-dimensional row array.
12. Handling File Paths and Errors
File operations can fail if a file does not exist, if the path is incorrect, or if the contents cannot be interpreted as numerical data.
Common File Errors
- FileNotFoundError: The specified file path does not exist.
- ValueError: The file contains values that cannot be parsed as requested.
- Incorrect delimiter: Values are separated differently from the delimiter specified.
- Unexpected header: Text headers are included when numerical data is expected.
Example: Handling a Missing File
Handling file errors allows a program to display a useful message rather than terminating unexpectedly.
13. Practical Activity: Student Marks File
Activity Description
At CIIT TRANING INSTITUTE, Samadhan, Pradnya, and Samiksha have marks in Python, NumPy, and Statistics. Save the marks to a CSV file, load them again, and calculate each student's total and average.
Sample Solution
Output:
The program exports the marks, loads them from the CSV file, and calculates the total and average for each student.
14. Common Mistakes
Mistake 1: Forgetting the Delimiter
When reading comma-separated values, specify delimiter="," so NumPy interprets the file correctly.
Mistake 2: Loading a Header as Numerical Data
Use skiprows when the file begins with a textual header that should not be read as a numerical row.
Mistake 3: Using the Wrong Data Type
Choose an appropriate dtype for the contents of the file. Integer types cannot represent fractional values exactly.
Mistake 4: Using an Incorrect File Path
Check the current working directory or supply the correct relative or absolute path to the file.
Mistake 5: Expecting the Same Display Format
Use the fmt argument in np.savetxt() when the output requires a specific number of decimal places or integer formatting.
15. Practice Exercises
Exercise 1: Load a Text File
Create a file containing three rows of numerical data. Load it using np.loadtxt() and display the array shape.
Exercise 2: Read CSV Data
Create a comma-separated file and load it using delimiter=",". Display the resulting NumPy array.
Exercise 3: Export an Array
Create a 3 × 3 NumPy array and save it to a text file using np.savetxt().
Exercise 4: Format Decimal Values
Save an array of decimal values using fmt="%.2f". Open the resulting file and inspect the formatting.
Exercise 5: Save and Reload Student Marks
Create marks for Samadhan, Pradnya, and Samiksha. Save the marks to a CSV file with a header, reload the numerical values, and calculate row-wise averages.
Exercise 6: Handle a Missing File
Try loading a file that does not exist. Use try and except to handle FileNotFoundError.
Summary
In this lesson, you learned how to load numerical text
data using np.loadtxt() and export NumPy
arrays using np.savetxt().
You explored delimiters, headers, comment lines, data types, and output formatting. You also practised saving and reloading student marks from a CSV file and calculating numerical summaries.