![]() |
VOOZH | about |
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.
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.
['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
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.
Output:
['First line\n', 'Second line\n', 'Third line\n']
List comprehension provides a concise way to read file lines into an array in a single line of code.
Output(example file content):
['First line\n', 'Second line\n', 'Third line\n']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.
Output( example file content)
['First line', 'Second line', 'Third line']If we need to work with binary data instead of text we can open the file in binary mode ('rb').
Output(example file content)
[b'First line\n', b'Second line\n', b'Third line\n']If we are working with numerical data the numpy.loadtxt() function can read file contents directly into a NumPy array.
Output(example file content)
['First' 'line' 'Second' 'line' 'Third' 'line']