LoginSignup
3
1

More than 5 years have passed since last update.

【Python】atomのUnicodeEncodeError, UnicodeDecodeErrorを防ぐ

Last updated at Posted at 2019-03-12

目次

問題

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です。
スクリーンショット 2019-03-14 23.04.41.png

また,起動スクリプトに以下のように書き込む対処法がよく見られますが,この方法だと問題2が解決できませんでした。

init.coffee
process.env.PYTHONIOENCODING = "utf-8";

参考

3
1
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
3
1