Best 5 Ways to Read a Text File into a List of Numbers in Python

In this article, we’ll explore how to read numbers from a text file and store them as lists of integers, suitable for applications like coordinate manipulation, using Python.

Problem Formulation

πŸ’‘ Goal: Transform textual representations of numbers in a text file into a Python list, where each line becomes a sublist of integers.

Example input file: πŸ‘‡

0 0 3 50
50 100 4 20

Example Python output: πŸ‘‡

[[0, 0, 3, 50], [50, 100, 4, 20]]

The result is a list of lists (nested list) of integers.

Let’s explore several methods to achieve this.

Method 1: Using a For Loop and Split

A simple for loop, coupled with string splitting and conversion, should do the trick:

my_list = []
with open('data.txt') as f:
    for line in f:
        line = line.split()
        if line:
            line = [int(i) for i in line]
            my_list.append(line)

This code snippet reads the file line by line, splits each line into a list of strings, converts each string into an integer, and appends the resulting list of integers to my_list.

πŸ‘‰ Recommended: How to Read a File Line-By-Line and Store Into a List?

Method 2: List Comprehension with Map

An elegant Pythonic way to accomplish our task using list comprehension and the map function:

with open('data.txt', "r") as infile:
    my_list = [list(map(int, line.split())) for line in infile if line.strip()]

This one-liner performs a similar operation as the first method but in a more compact form. It reads all the non-blank lines, and uses map to apply the int function to every element obtained after splitting the line. Since map returns an iterator in Python 3, we need to convert it to a list.

πŸ‘‰ Iterators, Iterables and Itertools

Method 3: Using Filter to Skip Blank Lines

We can incorporate filter to explicitly exclude blank lines:

with open('data.txt') as f:
    my_list = [list(map(int, x.split())) for x in f if x.strip()]

By using the condition if x.strip(), we ensure that only non-empty lines are processed. Each remaining line is split into substrings, and each substring is converted into an integer.

πŸ‘‰ How to Read a File Without Newlines in Python?

Method 4: Using the CSV module

For files that are structured in a consistent, tabular format, the CSV reader can be surprisingly versatile:

import csv

with open('data.txt', 'r') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=' ')
    polyShape = [[int(item) for item in row] for row in csvreader if row]

Although not a comma-separated file, setting the delimiter to a space allows the CSV reader to process our file. Each line is read as a list of strings and then converted to a list of integers.

πŸ‘‰ How to Read a CSV File Into a Python List?

Note that all examples above will raise a ValueError if there are non-numeric entities in the lines being read.

Always ensure that the input file strictly contains numeric values, or add appropriate error handling depending on your use case: πŸ‘‡πŸ‘‡πŸ‘‡

Method 5: Robust Parsing with Error Handling

When dealing with noisy input, you’ll want a method that’s resilient to erroneous data.

The following method includes error handling to manage non-numeric values without halting the execution:

def robust_numerical_line_parser(line):
    return [int(item) for item in line.split() if item.isdigit()]

my_list = []
with open('data.txt', 'r') as f:
    for line in f:
        try:
            numbers = robust_numerical_line_parser(line)
            if numbers:
                my_list.append(numbers)
        except ValueError as e:
            print(f"Encountered an error: {e} - in line: {line}")

This approach begins by attempting to parse each line into a list of integers, while silently ignoring any items that cannot be turned into integers.

If an unexpected type of error occurs, instead of crashing, it prints out an error message allowing the program to continue. Although this keeps the program robust, be aware that it can lead to loss of data if non-integer entries are expected.

Appending this method to our toolbox allows us to confront a wider range of situations, including cases where the input may not be pristine and could contain non-numeric characters.

This is especially useful when you have no control over the input file’s contents or when the cost of a crash is high.

Summary/Discussion

Each method provided is an effective way to read numbers from a text file into a list. The choice between them might depend on factors like personal preference for conciseness (methods 2 and 3) versus readability (method 1), or potential compatibility with more complex file formats (method 4). The most robust method that also handles some inappropriate inputs is Method 5.