27
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mecab-python3でUnicodeDecodeErrorがでる

Posted at

Unicodeの問題から逃げたくて、Python3にしたのに、mecab-python3の調子がおかしいのかUnicodeDecodeErrorがでる

しかも、テストを走らせるとランダムに出る

事例

import MeCab
mecab = MeCab.Tagger()
node = mecab.parseToNode("すもももももももものうち")
while node:
    print(node.surface)
    node = node.next

とすると、

UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-11-1f88b1ec9c08> in <module>()
      1 while node:
----> 2     print(node.surface)
      3     node = node.next

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte

というエラーが・・・

治し方

治し方もなかなかなぞで、先にmecab.parse("")をやっておくという

import MeCab
mecab = MeCab.Tagger()
mecab.parse("")  # 追加
node = mecab.parseToNode("すもももももももものうち")
while node:
    print(node.surface)
    node = node.next

すると

すもも
も
もも
も
もも
の
うち

できた。よくわからないけど、動くようになった。

27
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?