| by Arround The Web | No comments

Python Read File into List

Working with files (File handling) is a crucial aspect of programming that every developer must be comfortable with. When working with files, the data is sometimes needed to be placed inside arrays or lists for further processing or other requirements. In Python, this feat can be achieved by using multiple different methods, which include a combination of the read() method with the split() method, the readlines() method, and the loadtxt() method.

Let’s move on to the first method.

Method 1: File.read() Method With string.split() Method

This method seems full of commotion, but in reality, it is quite simple. The read() method is used to read textual data from a file, and the split() method is used to split a string into individual elements of a list. To demonstrate this, we have the following placed at the same location as the Python code:


Now, to read this file and convert it into a list, use the following lines of code:

file = open("readMe.txt," "r")
data = file.read()

print("\nData of the File: ", data)

listVar = data.split(",")
print("\nData Converted into List : ", listVar)
file.close()

 
In this code snippet:

    • The file “readMy.txt” is read and stored inside the “file” variable.
    • After that, the read() method is used to read its data and place it inside the “data” variable.
    • The content of the file is printed onto the console as it is using the print() function.
    • The split() method is used to split the data on every occurrence of a comma, “”, and this list is stored in the listVar variable.
    • The list is printed onto the terminal, and the access to the file is closed using the close() method.

When this code is executed, it produces the following result in the terminal:


The output verifies that the file has been read into a list in Python.

Method 2: The readlines() Method

If the data of the file expands to multiple lines and you want each individual line to be a separate entry in the list, then you can use the readlines() method. The readlines() method reads the file’s data line by line and places it inside a new entry in the list. To demonstrate its working, we have the following content of the file:


To read this data into a list with the help of the readlines() method, use the following lines of code:

file = open("readMe.txt","r")
data = file.readlines()
   
print("\nData of the File in List: ", data)
file.close()

 
When this code is executed, it produces the following result on the terminal:


The data has indeed been converted into a list. However, the output contains the escape sequence “\n”. To remove this, modify the according as shown below:

file = open("readMe.txt","r")
data = file.readlines()
   
print("\nData of the File in List: ", data)

newList = [];
for item in data:
    newList.append(item.replace("\n",""))
print("New List: ",newList)
file.close()

 
In this modified code, each element of the list “data” is added to a new list variable “newList” after applying the replace() method to remove the “\n” from the end. When this code is executed, it produces the following output:


The data of the file has been read into a file successfully in Python using the readlines() method.

Method 3: Using loadtxt() Method From Numpy

If the file that you want to read contains only numeric data, then it is best to use the loadtxt() method from the Numpy package. To demonstrate this, the following file will be used:


To demonstrate the working of the loadtxt() method to read file data into a list, use the following lines of code:

from numpy import loadtxt
file = open("readMe.txt","r")
data = loadtxt(file,delimiter=",")
   
print("\nData of the File in List: ", data)

 
In this code snippet:

    • Start by importing the loadtxt method from the numpy package.
    • Read the file using the open() method.
    • Use the loadtxt() method by passing in the file variable and in the second argument pass the delimiter that will be used to split the string as “,”.
    • Lastly, print the result stored in the list using the print() method.

When this code is executed, it produces the following results in the terminal:


You have successfully read numeric data into a list from a file using the loadtxt() method.

Conclusion

To read a file into a list in Python, the user can use the loadtxt() method from the Numpy library, the readlines() method, or the read() with the split() method. The loadtxt() method is best suitable for numeric data. The readlines() method is best suited for files that have content spread over multiple lines. Lastly, the combination of the read() and the split() is best for reading a string and converting it into a list upon encountering a delimiter.

Share Button

Source: linuxhint.com

Leave a Reply