*****使用完整代码编辑******
我正在尝试使用 Python(版本 3.5.3)和 MacOS 上的 MeCab 库解析一些日语代码。
我有一个包含以下文本的 txt 文件:
石の上に三年
我在我的 textEdit 上设置了我的偏好以使用 utf-8 保存。所以我相信系统正确地将其保存为 utf-8 格式。
我收到以下错误:
Traceback (most recent call last): File "japanese.py", line 29, in <module>
words = extractMetadataFromTXT(fileName) File "japanese.py", line 14, in extractMetadataFromTXT
md = extractWordsJP(data) File "japanese.py", line 22, in extractWordsJP
components.append(parsed.surface) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 0: invalid start byte
贝娄是我的完整代码。什么都没有。
import MeCab
import nltk
from nltk import *
from nltk.corpus import knbc
mt = MeCab.Tagger("-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd")
wordsList = knbc.words()
fdist = nltk.FreqDist(w.lower() for w in wordsList)
def extractMetadataFromTXT(filePath):
with open(filePath, 'r', encoding='utf-8') as f:
data = f.read()
print(data)
md = extractWordsJP(data)
print(md)
return md
def extractWordsJP(wordsJP):
components = []
parsed = mt.parseToNode(wordsJP)
while parsed:
components.append(parsed.surface)
parsed = parsed.next
return components
if __name__ == "__main__":
fileName = "simple_japanese.txt"
words = extractMetadataFromTXT(fileName)
print(words)
有人知道为什么我会收到此错误消息吗?
有趣的事实:有时它有效。:O
提前致谢,
以色列