How to Read Text Files into a List or Array with Python?

Writing, reading, and creating files are all incorporated into Python. Binary files (written in binary language, 0s and 1s) and text files are the two types of files that may be processed in Python. There are six different ways to access files.

Read only ('r') is the command used to open a text file for reading. The document's handle appears at the start.

Python provides many methods for reading a text file into a list or array.

Making Use of the open() Function

The open() method creates a file object from an open file. The open() method receives two parameters: the filename and the mode.

Example:

This is an example of how to use the open() function to open a file in read-only mode. Now, the read() method is used to read the file. The print() method is then used to output the file's data.

Output:

 
Coding encourages you to use logic and algorithms to create a program.
When facing a new challenge, you need to follow a logical approach to solve the issue.
Therefore, this is an exercise for your brain to train up your logical ability.   

Using load() Method from NumPy

Numpy.load() is a Python function that loads data from a text file in order to serve as a fast reader for basic text files. The open() method receives two parameters: the filename and the mode.

Example 1:

The text file is read into the numpy array and loadtxt is imported from the numpy module in the example that follows. The print() method is used to output the data into the list.

Output:

 
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14.]
Float64   

Explanation:

This Python program reads data from a text file called "example.txt" and loads it into a NumPy array using NumPy's loadtxt method. NumPy assumes that the data in the file is floating point numbers since it isn't stated explicitly as an integer data type. This leads to an array of floating point values. The array and its data type are then printed by the program, displaying the file's contents as a one-dimensional array of floating-point values.

One useful aspect of loadtxt() is that it lets us select the data type when importing the text file. Let's use an integer to indicate the text file to be imported into a NumPy array.

Example 2:

The loadtxt file is imported from the numpy module in the example that follows. The loadtxt() method helps read the text file into the numpy array. The print() method is then used to output the data into the list.

Output:

 
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14]
int32   

Explanation:

This Python programme reads data from a text file called "example.txt" and loads it into a NumPy array with an integer data type using NumPy's loadtxt method. The file's content is subsequently shown as a one-dimensional array of integers when the array and its data type are printed.

Using data.replace() Method

Pandas was used to construct the dataframe. The replace() method in a dataframe may be used to swap out a number, series, text, regex, list, dictionary, and more. This is a very rich function because of its many versions.

Example:

The file is opened in read mode and read using the read() method in the example that follows. If another ". is encountered, the text is divided and the line ending "n" is changed to "." The print() method is now used to print the data as output.

Output:

 
[' Coding encourages you to use logic and algorithms 
to create a program, 'When facing a new challenge, you need to follow a logical 
approach to solve the issue', 'Therefore, this is an exercise for your brain to 
train up your logical ability', ' Logical thinking is not only about solving 
algorithms but also beneficial to your personal and professional life, '']   

Explanation:

This Python programme reads the contents of a text file called "example.txt" in read-only mode. It then splits the text into a list whenever a period (.) is found, replacing line breaks with empty strings. It outputs the resultant list at the end.