pythonつまずき日記。今回はunicode encode error。ほんとこのエラー多くて大変。
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-19: ordinal not in range(128)
問題点
タイトルのエラーが出て困った。確認してみるとここがASCIIなのが問題のよう。環境は今どきpython2.X系。
>>> import sys
>>> print sys.getdefaultencoding()
ascii
>>> print sys.getdefaultencoding()
ascii
対処法
Pythonで日本語のエンコードエラーが発生したときの対応策
Pythonで日本語を使っていて、エンコード関連のエラーが出ました。解決策を簡単にまとめました。近年は自然言語処理などで日本語を扱うケースが増えてきているのかなと思いますが、日本語周りのエラーには悩まされそうです。
ここをみて
import io,sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding=’utf-8′)
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding=’utf-8′)
これやるといいよって書いてあるけどpython2だとbufferがないみたいで
AttributeError: ‘file’ object has no attribute ‘buffer’
と言われてうまくできない
Pythonの日本語処理
を参考にデフォルトのエンコーディングを設定する。
治った!ん、直った!
コメント