크기가 1.2GB 인 텍스트 파일의 단어 빈도를 계산하려고하는데 약 2 억 3 천만 단어였습니다. 다음 Python 코드를 사용하고 있습니다. 그러나 그것은 나에게 메모리 오류를 준다. 이것에 대한 해결책이 있습니까?Word 파이썬에서 1Gb 텍스트 파일의 빈도 계산
여기 내 코드입니다 :
import re
# this one in honor of 4th July, or pick text file you have!!!!!!!
filename = 'inputfile.txt'
# create list of lower case words, \s+ --> match any whitespace(s)
# you can replace file(filename).read() with given string
word_list = re.split('\s+', file(filename).read().lower())
print 'Words in text:', len(word_list)
# create dictionary of word:frequency pairs
freq_dic = {}
# punctuation marks to be removed
punctuation = re.compile(r'[.?!,":;]')
for word in word_list:
# remove punctuation marks
word = punctuation.sub("", word)
# form dictionary
try:
freq_dic[word] += 1
except:
freq_dic[word] = 1
print 'Unique words:', len(freq_dic)
# create list of (key, val) tuple pairs
freq_list = freq_dic.items()
# sort by key or word
freq_list.sort()
# display result
for word, freq in freq_list:
print word, freq
그리고 여기에 오류, 내가받은 :
Traceback (most recent call last):
File "count.py", line 6, in <module>
word_list = re.split('\s+', file(filename).read().lower())
File "/usr/lib/python2.7/re.py", line 167, in split
return _compile(pattern, flags).split(string, maxsplit)
MemoryError