リンク
ちゃんと調べるときは
http://www.python.jp/doc/release/
Wiki内検索
最近更新したページ
最新コメント
文字コードの判定 by awesome things!
正規表現 by awesome things!
辞書 by stunning seo guys
文字列の16進変換 by check it out
文字コードの変換 by check this out
デバッガ by check this out
サブプロセス by watch for this
文字コードの判定 by watch for this
サニタイジング by check this out
タグ

ソースの文字コード

ソースファイルの文字コード指定


ソースファイルの一行目に、
# -*- coding: utf_8 -*-
を書く。

一行目が
#!/usr/local/bin/python
などの場合は二行目に書く

Python2.3以降は
# coding: utf8
でも可

utf-8以外は以下
http://www.python.jp/doc/release/lib/standard-enco...

デフォルトの文字コードを指定


方法は二つある

sitecustomize.pyを作る


Pythonをどこにインストールしたかなんて生まれて一度も気にしたことがないので、findで探す。

$ find /usr -name site-packages -type d
/usr/lib/python2.4/site-packages
/usr/lib64/python2.4/site-packages
$
上記の場所に、sitecustomize.pyという名前で以下の内容のファイルを作る。

import sys
sys.setdefaultencoding('utf-8')

site.pyを直す。


Pythonをどこにインストールしたかなんて生まれて一度も気にしたことがないので、findで探す。

$ find /usr -name site.py
/usr/local/lib/python2.5/site.py
$

このファイルの370行目の
    370:     encoding = "ascii" # Default value set by _PyUnicode_Init()

    370:     encoding = "utf-8" # Default value set by _PyUnicode_Init()
にする。
2009年04月12日(日) 09:48:00 Modified by hebi_




スマートフォン版で見る