- Python - 文本处理
- Python - 文本处理简介
- Python - 文本处理环境
- Python - 字符串不变性
- Python - 排序行
- Python - 段落重新格式化
- Python - 统计段落中的词元
- Python - 二进制ASCII转换
- Python - 字符串作为文件
- Python - 反向读取文件
- Python - 过滤重复单词
- Python - 从文本中提取电子邮件
- Python - 从文本中提取URL
- Python - 美化打印
- Python - 文本处理状态机
- Python - 首字母大写和翻译
- Python - 词元化
- Python - 删除停用词
- Python - 同义词和反义词
- Python - 文本翻译
- Python - 单词替换
- Python - 拼写检查
- Python - WordNet 接口
- Python - 语料库访问
- Python - 词性标注
- Python - 块和块间隙
- Python - 块分类
- Python - 文本分类
- Python - 二元语法
- Python - 处理PDF
- Python - 处理Word文档
- Python - 读取RSS Feed
- Python - 情感分析
- Python - 搜索和匹配
- Python - 文本处理
- Python - 文本换行
- Python - 频率分布
- Python - 文本摘要
- Python - 词干提取算法
- Python - 受约束的搜索
Python - 反向读取文件
我们通常读取文件时,内容是从文件开头一行一行读取的。但有些情况下,我们可能需要先读取最后一行。例如,文件中的数据最新的记录在底部,而我们想先读取最新的记录。为实现此需求,我们使用以下命令安装所需的包来执行此操作。
pip install file-read-backwards
但在反向读取文件之前,让我们一行一行读取文件内容,以便我们可以比较反向读取后的结果。
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]
运行上述程序后,我们将得到以下输出:
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: # getting lines by lines starting from the last line up 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.
您可以验证行是否以反向顺序读取。
反向读取单词
我们还可以反向读取文件中的单词。为此,我们首先反向读取行,然后使用反向函数对其进行词元化。在下面的示例中,我们使用该包和nltk模块从同一个文件中反向打印单词词元。
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)
运行上述程序后,我们将得到以下输出:
['.', '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']
广告