【发布时间】:2018-02-02 23:17:29
【问题描述】:
我正在尝试腌制 {word : {docId : int}} 形式的字典。我的代码如下:
def vocabProcess(documents):
word_splitter = re.compile(r"\w+", re.VERBOSE)
stemmer=PorterStemmer()#
stop_words = set(stopwords.words('english'))
wordDict = {}
for docId in documents:
processedDoc = [stemmer.stem(w.lower()) for w in
word_splitter.findall(reuters.raw(docId)) if not w in stop_words]
for w in processedDoc:
if w not in wordDict:
wordDict[w] = {docId : processedDoc.count(w)}
else:
wordDict[w][docId] = processedDoc.count(w)
with open("vocabListings.txt", "wb") as f:
_pickle.dump(wordDict, f)
if __name__ == "__main__":
documents = reuters.fileids()
with open("vocabListings.txt", "r") as f:
vocabulary = _pickle.load(f)
当我运行这段代码时,我得到了错误
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2399:
character maps to <undefined>
当所有路透社文档/文档中没有 unicode 时,为什么会出现这种情况?我该如何解决这个问题,以便我仍然可以使用 _pickle 模块?
【问题讨论】:
标签: python pickle python-unicode