CentOSに python 日本語コード変換ライブラリー pykf をインストールする。

pythonで、入力データの文字コードを UTF-8,ShiftJIS, EUC-JP, JIS を相互に変換する必要が出て来たので、 python 日本語コード変換ライブラリー pykf をインストールしてみる。

1.インストール

easy_install pykf

2.使い方
http://sourceforge.jp/projects/pykf/docs/readme.jp/ja/1/readme.jp.txt

3.例、、テキストの文字コードを判別して、Unicodeに変換する例。

import pykf

def car_cnv(text):

	c = pykf.guess(text)
	if c is pykf.EUC:
		text = unicode(text, 'euc-jp', 'ignore')
	elif c is pykf.UTF8:
		text = unicode(text, 'utf-8', 'ignore')
	elif c is pykf.SJIS:
		text = unicode(text, 's-jis', 'ignore')
	elif c is pykf.JIS:
		text = unicode(text, 'iso-2022-jp', 'ignore')
	return text