「PythonのUnicodeDecodeErrorの対処方法」の版間の差分

提供: Python入門
移動: 案内検索
 
行69: 行69:
 
Exit 1
 
Exit 1
 
</syntaxhighlight>
 
</syntaxhighlight>
=== 解決方法 ===
+
=== 解決方法1 ===
 
出力先が端末、ファイルに関わらず、特定のエンコーディングで出力したい場合は、標準出力で書き換えてしまうのが簡単です。
 
出力先が端末、ファイルに関わらず、特定のエンコーディングで出力したい場合は、標準出力で書き換えてしまうのが簡単です。
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
import sys, codecs
 
import sys, codecs
 
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
 
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
 +
</syntaxhighlight>
 +
=== 解決方法2 ===
 +
解決方法1で解決しなかった場合は、コードの変更はなく、環境変数を設定するだけで対応できました。
 +
<syntaxhighlight lang="bash">
 +
env LC_ALL=en_US.UTF-8 /usr/bin/python ./doit.py
 
</syntaxhighlight>
 
</syntaxhighlight>
 
== 関連項目 ==
 
== 関連項目 ==

2018年5月23日 (水) 22:30時点における最新版

PythonのHTMLParserを使用して、HTMLをパースしていたときに UnicodeDecodeError(ユニコード デコード エラー) のエラーに遭遇しました。UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 6: ordinal not in range(128)

読み方

UnicodeDecodeError
ゆにこーど でこーど えらー

概要

以下のエラーメッセージが出現しました。

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 6:
ordinal not in range(128)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 6: ordinal not in range(128)

エラーが起きたとき

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 6:
ordinal not in range(128)

ASCII(アスキー)をデコードしようと思ったけど、できなかった、ということのようです。

プログラムを実行するとこのようになります。

$ python parse.py
Traceback (most recent call last):
  File "parse.py", line 210, in <module>
    parser.feed(html)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 108, in feed
    self.goahead(0)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 148, in goahead
    k = self.parse_starttag(i)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 252, in parse_starttag
    attrvalue = self.unescape(attrvalue)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 393, in unescape
    return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities, s)
  File "/usr/local/lib/python2.7/re.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 6:
 ordinal not in range(128)
Exit 1

解決方法

foo ( str );

なところを

foo ( str.decode('utf-8') );

とするだけで、解決しました。

UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-6: ordinal not in range(128)

エラーが起きたとき

出力をリダイレクトしない場合は、問題にならなかったのですが、リダイレクトした瞬間に

UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-6:
ordinal not in range(128)

とエラーが出るようになりました。

% python ./parse.py < foo.txt
% python ./parse.py < foo.txt > bar.txt
Traceback (most recent call last):
  File "./parse.py", line 210, in <module>
    parser.feed(html.decode('utf-8') )
  File "/usr/local/lib/python2.7/HTMLParser.py", line 108, in feed
    self.goahead(0)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 150, in goahead
    k = self.parse_endtag(i)
  File "/usr/local/lib/python2.7/HTMLParser.py", line 319, in parse_endtag
    self.handle_endtag(tag.lower())
  File "./parse.py", line 128, in handle_endtag
    print self.expression, ",", self.meaning,
UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-6: ordinal not in range(128)
Exit 1

解決方法1

出力先が端末、ファイルに関わらず、特定のエンコーディングで出力したい場合は、標準出力で書き換えてしまうのが簡単です。

import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

解決方法2

解決方法1で解決しなかった場合は、コードの変更はなく、環境変数を設定するだけで対応できました。

env LC_ALL=en_US.UTF-8 /usr/bin/python ./doit.py

関連項目