Python - Rückwärtslesen von Dateien

Wenn wir normalerweise eine Datei lesen, wird der Inhalt vom Anfang der Datei Zeile für Zeile gelesen. Es kann jedoch Szenarien geben, in denen wir die letzte Zeile zuerst lesen möchten. Zum Beispiel haben die Daten in der Datei unten den neuesten Datensatz und wir möchten zuerst die neuesten Datensätze lesen. Um diese Anforderung zu erfüllen, installieren wir das erforderliche Paket, um diese Aktion auszuführen, indem wir den folgenden Befehl verwenden.

pip install file-read-backwards

Bevor wir die Datei jedoch rückwärts lesen, lesen wir den Inhalt der Datei Zeile für Zeile, damit wir das Ergebnis nach dem Rückwärtslesen vergleichen können.

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

Wenn wir das obige Programm ausführen, erhalten wir die folgende Ausgabe:

Line No-  0
Vito Corleone is the aging don (head) of the Corleone Mafia Family. 
Line No-  1
His youngest son Michael has returned from WWII just in time to see the wedding of Connie Corleone (Michael's sister) to Carlo Rizzi. 
Line No-  2
All of Michael's family is involved with the Mafia, but Michael just wants to live a normal life. Drug dealer Virgil Sollozzo is looking for Mafia families to offer him protection in exchange for a profit of the drug money. 
Line No-  3
He approaches Don Corleone about it, but, much against the advice of the Don's lawyer Tom Hagen, the Don is morally against the use of drugs, and turns down the offer.
Line No-  4
This does not please Sollozzo, who has the Don shot down by some of his hit men. 
Line No-  5
The Don barely survives, which leads his son Michael to begin a violent mob war against Sollozzo and tears the Corleone family apart.

Zeilen rückwärts lesen

Um die Datei nun rückwärts zu lesen, verwenden wir das installierte Modul.

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

Wenn wir das obige Programm ausführen, erhalten wir die folgende Ausgabe:

The Don barely survives, which leads his son Michael to begin a violent mob war against Sollozzo and tears the Corleone family apart.
This does not please Sollozzo, who has the Don shot down by some of his hit men. 
He approaches Don Corleone about it, but, much against the advice of the Don's lawyer Tom Hagen, the Don is morally against the use of drugs, and turns down the offer.
All of Michael's family is involved with the Mafia, but Michael just wants to live a normal life. Drug dealer Virgil Sollozzo is looking for Mafia families to offer him protection in exchange for a profit of the drug money. 
His youngest son Michael has returned from WWII just in time to see the wedding of Connie Corleone (Michael's sister) to Carlo Rizzi. 
Vito Corleone is the aging don (head) of the Corleone Mafia Family.

Sie können überprüfen, ob die Zeilen in umgekehrter Reihenfolge gelesen wurden.

Wörter rückwärts lesen

Wir können die Wörter in der Datei auch rückwärts lesen. Dazu lesen wir zuerst die Zeilen rückwärts und markieren dann die darin enthaltenen Wörter mit der Umkehrfunktion. Im folgenden Beispiel werden Wort-Token aus derselben Datei mit dem Paket- und dem nltk-Modul rückwärts gedruckt.

import nltk
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
# And tokenizing with applying reverse()
    for line in BigFile:
        word_data= line
        nltk_tokens = nltk.word_tokenize(word_data)
        nltk_tokens.reverse()
        print (nltk_tokens)

Wenn wir das obige Programm ausführen, erhalten wir die folgende Ausgabe -

['.', 'apart', 'family', 'Corleone', 'the', 'tears', 'and', 'Sollozzo', 'against', 'war', 'mob', 'violent', 'a', 'begin', 'to', 'Michael', 'son', 'his', 'leads', 'which', ',', 'srvives', 'barely', 'Don', 'The']
['.', 'men', 'hit', 'his', 'of', 'some', 'by', 'down', 'shot', 'Don', 'the', 'has', 'who', ',', 'Sollozzo', 'please', 'not', 'does', 'This']
['.', 'offer', 'the', 'down', 'trns', 'and', ',', 'drgs', 'of', 'se', 'the', 'against', 'morally', 'is', 'Don', 'the', ',', 'Hagen', 'Tom', 'lawyer', "'s", 'Don', 'the', 'of', 'advice', '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', 'live', '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', 'see', 'to', 'time', 'in', 'jst', 'WWII', 'from', 'retrned', 'has', 'Michael', 'son', 'yongest', 'His']
['.', 'Family', 'Mafia', 'Corleone', 'the', 'of', ')', 'head', '(', 'don', 'aging', 'the', 'is', 'Corleone', 'Vito']