目次
問題
atomの「script」パッケージなどを使ってpythonを実行すると,UnicodeError
が発生する。
1. UnicodeEncodeError
「unicode型」→「str型」変換の際に発生するエラー
print('こんにちは')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
2. UnicodeDecodeError
「str型」→「unicode型」変換の際に発生するエラー
hello.txt
こんにちは
with open('hello.txt') as f:
f.read()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)
解決法
atomのロケール設定をja_JP.UTF-8
に統一してあげましょう。
init.coffee(メニューバー「Atom」>「Init Script.../起動スクリプト...」)に以下を追記して再起動
init.coffee
process.env.LANG = "ja_JP.UTF-8";
ちなみに
「script」パッケージのロケール設定のみja_JP.UTF-8
にする場合は,
メニューバー「パッケージ」>「Script」>「Configure Script」で以下のように設定して実行すればOKです。
また,起動スクリプトに以下のように書き込む対処法がよく見られますが,この方法だと問題2が解決できませんでした。
init.coffee
process.env.PYTHONIOENCODING = "utf-8";
参考