Reading From a File and Print in Reverse Order Line Python

Python - Backward File Reading


When we unremarkably read a file, the contents are read line by line from the kickoff of the file. But there may be scenarios where we want to read the terminal line first. For example, the information in the file has latest record in the lesser and we desire to read the latest records first. To achieve this requirement we install the required package to perform this action by using the command below.

pip install file-read-backwards        

But before reading the file backwards, let's read the content of the file line by line then that we tin compare the effect after backward reading.

with open ("Path\GodFather.txt", "r") every bit BigFile:     data=BigFile.readlines()  # Print each line 	for i in range(len(data)):     print "Line No- ",i      impress data[i]        

When we run the above program, we get the following output −

Line No-  0 Vito Corleone is the aging don (head) of the Corleone Mafia Family.   Line No-  ane His youngest son Michael has returned from WWII simply in fourth dimension to run into the wedding of Connie Corleone (Michael's sis) to Carlo Rizzi.   Line No-  2 All of Michael's family is involved with the Mafia, but Michael merely wants to live a normal life. Drug dealer Virgil Sollozzo is looking for Mafia families to offer him protection in substitution for a profit of the drug coin.   Line No-  3 He approaches Don Corleone nearly it, but, much against the communication of the Don'due south lawyer Tom Hagen, the Don is morally against the use of drugs, and turns down the offer.  Line No-  iv This does not please Sollozzo, who has the Don shot downwards by some of his hit men.   Line No-  v The Don barely survives, which leads his son Michael to begin a trigger-happy mob war confronting Sollozzo and tears the Corleone family autonomously.        

Reading Lines Backward

Now to read the file backwards we use the installed module.

from file_read_backwards import FileReadBackwards  with FileReadBackwards("Path\GodFather.txt", encoding="utf-8") as BigFile:  # getting lines by lines starting from the last line up     for line in BigFile:         print line        

When nosotros run the in a higher place program, we become the following output −

The Don barely survives, which leads his son Michael to brainstorm a tearing mob war against Sollozzo and tears the Corleone family autonomously. This does non please Sollozzo, who has the Don shot downwardly by some of his hit men.  He approaches Don Corleone about information technology, but, much against the communication of the Don'south lawyer Tom Hagen, the Don is morally against the use of drugs, and turns down the offer. All of Michael'south family is involved with the Mafia, but Michael just wants to alive a normal life. Drug dealer Virgil Sollozzo is looking for Mafia families to offer him protection in substitution for a turn a profit of the drug money.  His youngest son Michael has returned from WWII simply in fourth dimension to see the nuptials of Connie Corleone (Michael's sis) to Carlo Rizzi.  Vito Corleone is the crumbling don (head) of the Corleone Mafia Family unit.        

Yous can verify the lines accept been read in a reverse gild.

Reading Words Backward

We tin also read the words in the file backward. For this we first read the lines backwards and then tokenize the words in information technology with applying reverse function. In the below case we have word tokens printed backwards class the same file using both the package and nltk module.

import nltk from file_read_backwards import FileReadBackwards  with FileReadBackwards("Path\GodFather.txt", encoding="utf-viii") as BigFile:  # getting lines past lines starting from the terminal line up # And tokenizing with applying reverse()     for line in BigFile:         word_data= line         nltk_tokens = nltk.word_tokenize(word_data)         nltk_tokens.opposite()         print (nltk_tokens)        

When nosotros run the above programme we get the following output −

['.', 'apart', 'family', 'Corleone', 'the', 'tears', 'and', 'Sollozzo', 'against', 'war', 'mob', 'violent', 'a', 'begin', 'to', 'Michael', 'son', 'his', 'leads', 'which', ',', 'srvives', 'barely', 'Don', 'The'] ['.', 'men', 'hitting', 'his', 'of', 'some', 'by', 'downward', 'shot', 'Don', 'the', 'has', 'who', ',', 'Sollozzo', 'delight', 'not', 'does', 'This'] ['.', 'offer', 'the', 'downwardly', 'trns', 'and', ',', 'drgs', 'of', 'se', 'the', 'against', 'morally', 'is', 'Don', 'the', ',', 'Hagen', 'Tom', 'lawyer', "'s", 'Don', 'the', 'of', 'communication', 'the', 'against', 'mch', ',', 'bt', ',', 'it', 'abot', 'Corleone', 'Don', 'approaches', 'He'] ['.', 'money', 'drg', 'the', 'of', 'profit', 'a', 'for', 'exchange', 'in', 'protection', 'him', 'offer', 'to', 'families', 'Mafia', 'for', 'looking', 'is', 'Sollozzo', 'Virgil', 'dealer', 'Drg', '.', 'life', 'normal', 'a', 'alive', 'to', 'wants', 'jst', 'Michael', 'bt', ',', 'Mafia', 'the', 'with', 'involved', 'is', 'family', "'s", 'Michael', 'of', 'All'] ['.', 'Rizzi', 'Carlo', 'to', ')', 'sister', "'s", 'Michael', '(', 'Corleone', 'Connie', 'of', 'wedding', 'the', 'run into', 'to', 'time', 'in', 'jst', 'WWII', 'from', 'retrned', 'has', 'Michael', 'son', 'yongest', 'His'] ['.', 'Family unit', 'Mafia', 'Corleone', 'the', 'of', ')', 'head', '(', 'don', 'aging', 'the', 'is', 'Corleone', 'Vito']        

Useful Video Courses


Python Essentials Online Training

Video

Learn Python Programming in 100 Easy Steps

Video

Python with Data Science

Video

Python 3 from scratch to become a developer in demand

Video

Python Data Science basics with Numpy, Pandas and Matplotlib

Video

munozcaved1955.blogspot.com

Source: https://www.tutorialspoint.com/python_text_processing/python_backward_file_reading.htm

0 Response to "Reading From a File and Print in Reverse Order Line Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel