LoginSignup
9
5

More than 5 years have passed since last update.

Windows環境のpython で日本語出すと文字コードでエラーがでるときの対処方法。

Posted at

簡単なサンプルとしてWindowsに付属しているメモ帳などを開いて

jp_test.py
print ("日本語")

と記載して実行してみると・・・

C:\sample>python jp_test.py
File "jp_test.py", line 1
SyntaxError: Non-UTF-8 code starting with '\x93' in file jp_test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

というようなエラーになります。

これは、作成したファイルがANSI(SJIS)コードで作成されているからです。
そのため作成したファイルの文字コードをANSI→UTF-8に変更することでエラーがなくなります。

メモ帳だとこんな感じ。
jp_test.png

UTF-8で保存して!

C:\sample>python jp_test.py
日本語

となります。

#coding: UTF-8

って入れないとダメなのか?とか、

print (u"日本語")

ってしなければいけないのか?とか悩んだけど、何のことは無い保存したファイルの文字コードの問題だっただけという落ちでした・・・:stuck_out_tongue:

9
5
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
9
5