Python - Read Text File into List or Array
Reading files and storing their contents in an array is a common task in Python. It is mostly used when we need to process file data line by line or manipulate file data as a list of strings.In this article, we will explore different methods to read a file and store its contents in an array (list) efficiently.
Using readlines Method (Most Efficient)
The readlines() method reads all lines in a file and returns them as a list of strings making it the most efficient way to store file contents in an array.
# Open the file in read mode
with open('example.txt', 'r') as file:
# Read all lines into a list
lines = file.readlines()
print(lines)
Output (example file content):
['First line\n', 'Second line\n', 'Third line\n']Let's see some other methods to read files with an Array in Python
Table of Content
Using a Loop to Read Line by Line
This method allows us to read lines one at a time and append them to an array manually. It is useful for large files to avoid memory issues.
a = []
# Open the file in read mode
with open('example.txt', 'r') as file:
# Read and append each line to the list
for a in file:
a.append(line)
print(a)
Output:
['First line\n', 'Second line\n', 'Third line\n']
Using List Comprehension
List comprehension provides a concise way to read file lines into an array in a single line of code.
# Read lines using list comprehension
with open('example.txt', 'r') as file:
a = [line for line in file]
print(a)
Output(example file content):
['First line\n', 'Second line\n', 'Third line\n']Removing Newline Characters While Reading
To remove the trailing newline characters (\n) from each line while reading the file we can use strip() along with a loop or list comprehension.
# Read lines without newline characters
with open('example.txt', 'r') as file:
a = [line.strip() for line in file]
print(a)
Output( example file content)
['First line', 'Second line', 'Third line']Reading Files as Binary Data
If we need to work with binary data instead of text we can open the file in binary mode ('rb').
# Read binary data into a list of bytes
with open('example.txt', 'rb') as file:
a = file.readlines()
print(a)
Output(example file content)
[b'First line\n', b'Second line\n', b'Third line\n']Using NumPy to Read File into an Array
If we are working with numerical data the numpy.loadtxt() function can read file contents directly into a NumPy array.
import numpy as np
# Read file content into a NumPy array
array = np.loadtxt('example.txt', dtype=str)
print(array)
Output(example file content)
['First' 'line' 'Second' 'line' 'Third' 'line']