Python Read a File Into a List

Y'all may accept heard about file treatment in many programming languages. But have yous always tried it? If not, and so this guide is meant for y'all. In this article, we will be showing you lot some uncomplicated methods of python to read file data into a listing of data structures.

Example 01: read().split() Function

The very first method to read the file data into a list is using the python "read()" function along with the "split up()" function in the code. Open up a notepad file as a "txt" file and add some data to it.

We have created a variable file descriptor named "File". It will be using the open() function of python to open a file "new.txt" using its path in the parameter to make a list. The data of a file will be read out by a role read() and saved to a variable "data". The information of a file volition be printed out. Nosotros have created a listing of data with the help of a "split()" function, and the file descriptor will be closed. A newly created list will be printed out. Yous can modify the code to fulfill your specific requirements.

  • File = open( "D :/new.txt" , "r")
  • Information = File.read()
  • Print ("Information in file : " , information)
  • File.close()
  • Print ("List of information: " , List)

Subsequently running this code, we have got the as it is data of a file and so it a list of this data also.

Example 02: read.split up() Function

Let's have another type of data in a text file, i.e., string, as shown in the paradigm beneath.

To directly use the information of a file, we have used the "with" statement to open up a file in a read mode using the file descriptor "f". The read().split() function has been reading data from a file splitting them by "," sign and saved to the variable "Line". Every split string has been printed out separately with the help of a "for" loop then. You lot can modify the code to fulfill your specific requirements.

  • With open( "D :/new.txt" , "r") as f:
  • Line = f.read().separate( ' )
  • For L in Line
  • Print (L)

Nosotros accept got the contents of a file in a splitted list course.

Case 03: readlines() Office

A very similar method to read the file data into a list is using python'southward "readlines()" role in your code. Consider the below data in your new.txt file.

Begin with the cosmos of a file descriptor to open the file by utilizing its path and manner. The information has been read out as a list with the aid of a readlines() function and saved into a variable "List". The listing of file information has been printed out. Y'all can change the lawmaking to fulfill your specific requirements.

  • File = open up( "D :/new.txt" , "r")
  • List = File.read(lines)
  • Print (Listing)

The data of a file in the grade of a line has been read out in a list form.

Instance 04: loadtxt() Office

The last simply non the to the lowest degree method to read the file into a list is using the loadtxt() method of python. To utilise this technique, we need to apply the Numpy library in the code. Firstly, consider the below integer information in a file separated by commas.

Import the loadtxt module from a Numpy library of python. And so a variable "line" has been fabricated, which collects data from a file with the help of a "loadtxt()" method. Moreover, this function takes "," every bit a delimiter in its parameter subsequently taking the file path. The "for" loop will be very helpful here to print out the data one by one every bit a comma has been used here as a delimiter.

  • From numpy import loadtxt
  • Line = loadtxt( "D :/new.txt" , delimiter = " ' ")
  • For Fifty in Line
  • Print (50)

When we run this code, it gives the file data in the class of a splitted list as per the image beneath.

Conclusion:

This guide has been very helpful to all naïve users of python to get a articulate understanding of reading the file information into a list. Moreover, if one method doesn't work for you, you can use the other as an alternative.

Well-nigh the author

Howdy, I am a freelance writer and commonly write for Linux and other engineering related content

cappsfortalwyneho66.blogspot.com

Source: https://linuxhint.com/python-read-file-into-list/

0 Response to "Python Read a File Into a List"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel