LoginSignup
2
0

More than 5 years have passed since last update.

python3でUnicodeDecodeErrorが出た時の応対

Posted at

はじめに

開発サーバーでpythonバッチを実行したら以下のUnicodeDecodeErrorが出たのでその時の対処法をまとめました
スクリーンショット 2018-02-14 18.52.10.png

参考にしまくったもの

対応方法

  1. まずPython3でUnicodeDecodeErrorに遭遇したときのTODOリストを見て深呼吸して落ち着きブツブツつぶやきます
  2. 次にサーバー環境の環境言語を見ます。 スクリーンショット 2018-02-14 18.50.23.png
import sys
print(sys.stdout.encoding)

ANSIなのです。これまでのローカルでは utf8がデフォルトでした
3.エラー元をみる
スクリーンショット 2018-02-15 11.27.49.png
opneでtextを開いているが、文字コードを指定してないので、デフォルト(ANSI)に変わっていたようだ。

  1. codecsを導入してutf-8デコードを行う

pythonの標準エンコードクラスであるcodecsをimportしてきてopenする際にutf-8でデコードします

これで解決です!
スクリーンショット 2018-02-15 11.32.31.png

最後に

参考にさせていただきましたサイトさんの方々、ありがとうございました。おかげさまでうまくいきました。

2
0
2

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
2
0