8
4

More than 5 years have passed since last update.

pythonで「coding: utf-8」と書いたのにSyntaxErrorになる理由

Last updated at Posted at 2018-05-12

結論:ファイルのエンコード(名前付けて保存するときに出るあれ)がおかしいんですよ。

コメント欄に

 # -*- coding: utf-8 -*-

て書いてるのに

SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0x8a in position 0:
 invalid start byte

と出る。

そもそも自分の書いたファイルのエンコードがutf-8形式になっていないのが原因で他人のプログラムのコピペーで作ったらそりゃ動かないわなと言う反省。

そこで下記の通り変更した。

 # -*- coding: Shift-JIS -*-

動いたよ~。

そこでもう一つ。うちは、長年統合開発環境を使わずにEditor(「秀丸エディタ」)で開発しているので、utf-8で実行したい場合は、やはり「名前を付けて保存」で「エンコードの種類」を「Unicode(UTF-8)」を選択し直し「保存」する。
もしくは、「ファイル」→「エンコードの種類」→「Unicode(UTF-8)」にして保存しておけばOK。

8
4
1

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
8
4