Python – 反向读取文件
当我们通常读取文件时,文件内容是从文件开头逐行读取的。但有些情况下,我们想要先读取最后一行。比如,文件中的最新记录在底部,我们想要先读取最新的记录。为了实现这个要求,我们可以使用以下命令安装所需的包以执行此操作。
pip install file-read-backwards
但在反向读取文件之前,让我们先逐行读取文件的内容,以便在反向读取之后可以对比结果。
with open ("Path\GodFather.txt", "r") as BigFile:
data=BigFile.readlines()
# 逐行打印文件内容
for i in range(len(data)):
print "Line No- ",i
print data[i]
当我们运行上面的程序时,将得到以下输出:
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.
反向读取行
现在,为了反向读取文件,我们使用已安装的模块。
from file_read_backwards import FileReadBackwards
with FileReadBackwards("Path\GodFather.txt", encoding="utf-8") as BigFile:
# 从最后一行开始逐行获取数据
for line in BigFile:
print line
当我们运行上面的程序时,将得到以下输出:
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.
可以验证行已以相反的顺序被读取。
读取单词的反向拼写
我们也可以读取文件中的单词的反向拼写。为此,我们首先反向读取行,然后使用 reverse 函数进行单词分词。在下面的示例中,我们使用包和 nltk 模块打印了来自同一文件的单词标记的反向拼写形式。
import nltk
from file_read_backwards import FileReadBackwards
with FileReadBackwards("Path\GodFather.txt", encoding="utf-8") as BigFile:
# 从最后一行开始逐行获取,并使用 reverse() 进行标记化
for line in BigFile:
word_data= line
nltk_tokens = nltk.word_tokenize(word_data)
nltk_tokens.reverse()
print (nltk_tokens)
运行上述程序后,我们将得到以下输出 –
['.', 'The', 'Don', 'barely', 'srvives', ',', 'which', 'leads', 'his', 'son', 'Michael', 'to', 'begin', 'a', 'violent', 'mob', 'war', 'against', 'Sollozzo', 'and', 'the', 'Corleone', 'family', 'apart', '.']
['This', 'does', 'not', 'please', 'Sollozzo', ',', 'who', 'shot', 'down', 'some', 'of', 'his', 'men', 'by', 'hit', ',', 'and', 'Don', 'the', 'has', 'to', 'offer', 'him', 'protection', 'in', 'exchange', 'for', 'a', 'profit', 'of', 'the', 'drug', 'trade', '.']
['He', 'approaches', 'Don', 'Corleone', 'about', 'it', ',', 'but', ',', 'against', 'the', 'advice', 'of', 'his', 'lawyer', 'Tom', 'Hagen', ',', 'the', 'Don', 'is', 'morally', 'against', 'the', 'drug', 'trade', 'and', 'refuses', 'the', 'offer', '.']
['All', 'of', 'Michael', "'s", 'family', 'is', 'involved', 'with', 'the', 'Mafia', ',', 'but', 'Michael', 'wants', 'to', 'live', 'a', 'normal', 'life', 'and', 'is', 'looking', 'for', 'protection', 'for', 'families', 'in', 'exchange', 'for', 'the', 'profit', 'of', 'the', 'drug', 'trade', '.', 'Virgil', 'Sollozzo', 'is', 'a', 'dealer', 'of', 'drugs', 'with', 'the', 'Mafia', 'and', 'he', 'is', 'offering', 'to', 'exchange', 'money', 'for', 'a', 'profit', '.']
['His', 'youngest', 'son', 'Michael', 'has', 'returned', 'from', 'WWII', 'in', 'time', 'to', 'see', 'the', 'wedding', 'of', 'his', 'sister', 'Connie', 'to', 'Carlo', 'Rizzi', '(', 'Michael', '(', 'Corleone', ')', 'of', 'the', 'head', '(', 'don', ')', 'of', 'the', 'Mafia', 'Corleone', 'family', '.']
['Vito', 'Corleone', 'is', 'the', 'aging', 'don', '(', 'head', ')', 'of', 'the', 'Corleone', 'Mafia', 'family', '.']