アットウィキロゴ

Python で日本語

Python

python 2.3以降?でのお話し。 それ以前のバージョンでははコーディングのやり方が違うので注意。

ソースファイルのエンコーディングの指定

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

coding(:|=) utf-8 の前後は自由に記述できるので、 下のようにVIMの設定なんかと混合できる

# vim: fileencoding=utf-8

出力エンコーディングの指定

#!/usr/bin/env python
# vim: fileencoding=utf-8

import sys;
import codecs;

sys.stdout = codecs.getwriter('cp932')(sys.stdout);
print u"テスト\n";

文字コードの変換

#!/usr/bin/env python
# vim: fileencoding=utf-8

import sys;
import codecs;

print u"テスト\n".encode('cp932');
最終更新:2008年06月29日 21:04