我在阅读Python文件时遇到问题。
我在Python中读取的文件大小为90 Mb。当用word打开时,它表示单词总数约为1400万。但是当我用Python读取文件时,它给我的文件长度约为900万字(8,915,710字)。
当我通过python命令检查文件中的最后100个单词时
print "The length of the Corpus is ", len(tokens), tokens[-100:]
我只能从原始文件的中间找到单词。
我使用的是64位Windows操作系统和32位版本的Python。
PC规格:i7,1.8Gz,6GB RAM
我想理解为什么Python拒绝阅读超过8,915,710个单词。
由于
CODE:
f = open('testtext.txt')
raw = f.read()
corp = lowercase(raw)
tokens = nltk.word_tokenize(corp)
print "The number of words is ", len(tokens), tokens[-100:]
print "corp ", len(corp)
print "raw ", len(raw)
我得到以下答案:
>> The number of words is 8915710
>> corp 53322476
>> raw 53322476
答案 0 :(得分:1)
替换此行:
f = open('testtext.txt')
这一行:
f = open('testtext.txt', 'rb')
答案 1 :(得分:0)
尝试将文件处理为二进制文件:
f = open('file.txt', "rb")
chunkSize = 1024
dataChunk = f.read(chunkSize)
while len(dataChunk):
processData(dataChunk)
dataChunk = f.read(chunkSize)