β
Problem Formulation: Developers often need to read a space-delimited text file and process its contents as a list in Python. For example, given a file named data.txt
containing the line “apple banana cherry”, the goal is to produce the list ['apple', 'banana', 'cherry']
. This article outlines various methods to achieve this, catering to different scenarios and requirements.
Method 1: Using split() with File Handling
The most straightforward way to read a space-delimited text file into a list is by opening the file, reading its lines, and using the split()
method to divide each line into a list, using space as the default delimiter.
Here’s an example:
with open('data.txt', 'r') as file: list_of_words = file.readline().split() print(list_of_words)
In this example, the with
statement is used to open data.txt
, ensuring that the file is properly closed after its contents are read. The readline()
method reads a single line from the file, and split()
divides this line by spaces, resulting in a list of words.
π How to Convert Space-Delimited File to CSV in Python?
Method 2: Reading Multiple Lines
If the file contains multiple lines, you can iterate through each line and build a list of words using a list comprehension combined with the split()
method.
Here’s an example:
with open('data.txt', 'r') as file: list_of_words = [word for line in file for word in line.split()] print(list_of_words)
This code snippet uses a double for-loop within a list comprehension to iterate over each line in the file, then over each word within each line, adding them to list_of_words
as it goes.
π Best 5 Ways to Read a Text File into a List of Numbers in Python
Method 3: Using read().split()
For files with an unknown number of lines or when you want to read the whole file at once, use the read()
method followed by split()
.
Here’s an example:
with open('data.txt', 'r') as file: list_of_words = file.read().split() print(list_of_words)
This example reads the entire file into a single string using read()
, then splits the string into a list using split()
. This is more suitable for smaller files due to potential memory constraints with large files.
π How to Convert Tab-Delimited File to CSV in Python?
Method 4: Handling Empty Lines
Files often contain empty lines that you might want to ignore when creating your list. You can combine checking for empty lines with the split()
method to achieve this.
Here’s an example:
with open('data.txt', 'r') as file: list_of_words = [word for line in file if line.strip() for word in line.split()] print(list_of_words)
This code uses a list comprehension that checks if a line is not empty after stripping whitespace with line.strip()
before processing words with split()
.
Bonus One-Liner Method 5: Using splitlines() and split()
For a quick one-liner solution, you can use splitlines()
in combination with split()
to read all lines and split them by spaces at the same time.
Here’s an example:
list_of_words = open('data.txt').read().splitlines()[0].split() print(list_of_words)
This concise line reads the entire file, splits it into lines with splitlines()
, and takes the first line (index 0) to split into words, assuming the file contains a single, space-delimited line.
Summary/Discussion
- Method 1:
- Best for single-line files.
- Can waste memory if file contains a lot of unnecessary data.
- Method 2:
- Suitable for multi-line files.
- Can be less readable due to double for-loop comprehension.
- Method 3:
- Good for an unknown number of lines.
- Memory-intensive for very large files.
- Method 4:
- Useful for files with empty lines.
- Slightly more complex due to condition inside list comprehension.
- Method 5:
- Quick one-liner.
- Not suitable for multi-line files unless modified.