「Python ファイルの文字コード」の版間の差分
ナビゲーションに移動
検索に移動
| 1行目: | 1行目: | ||
| − | ==Python ファイルの文字コード== | + | ==[[Python ファイルの文字コード]]== |
| − | [[Python]] | | + | [[Python]] | [[Category:文字化け]] |
| − | === | + | ===ソースコード・[[エンコーディング]]=== |
| − | * | + | *ソースファイルには、ASCII以外の[[エンコーディング]]も利用できる。 |
| − | *そのためには、#! | + | *そのためには、#! 直後の行で[[エンコーディング]]定義を行うのがよい |
| − | #! | + | #![[Python]]2.6 |
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
*以下のようなエラーが出る場合、上記対処 | *以下のようなエラーが出る場合、上記対処 | ||
<blockquote>SyntaxError: Non-ASCII character '\xe3' in file test.py on line 6, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details</blockquote> | <blockquote>SyntaxError: Non-ASCII character '\xe3' in file test.py on line 6, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details</blockquote> | ||
| − | === | + | ===[[文字コード]]を指定して、ファイルを開く=== |
import codecs | import codecs | ||
fd = codecs.open(search_result_file, 'r', 'shift_jis') | fd = codecs.open(search_result_file, 'r', 'shift_jis') | ||
for l in fd: | for l in fd: | ||
print l | print l | ||
2020年2月16日 (日) 04:30時点における最新版
Python ファイルの文字コード
Python |
ソースコード・エンコーディング
#!Python2.6 # -*- coding: utf-8 -*-
- 以下のようなエラーが出る場合、上記対処
<blockquote>SyntaxError: Non-ASCII character '\xe3' in file test.py on line 6, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details</blockquote>
文字コードを指定して、ファイルを開く
import codecs
fd = codecs.open(search_result_file, 'r', 'shift_jis')
for l in fd:
print l
© 2006 矢木浩人