5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

# 文字列の文字コードを返す関数

Posted at

引数 msg の文字コードを探し当てる関数


def get_jp_encoding_name (msg,char_code = 'iso-2022-jp'):
	import types
	
	enc_name = char_code or 'iso-2022-jp'
	check_type = ['utf_8',
				   enc_name,		'iso2022_jp_1', 'iso2022_jp_2',
				   'iso2022_jp_2004','iso2022_jp_3', 'iso2022_jp_ext',
				   'shift-jis',	  'cp932',		'shift_jis_2004',
				   'shift_jisx0213', 'euc_jp',	   'euc_jis_2004',
				   'euc_jisx0213']
	for enc in check_type:
		try:
			if isinstance(msg, types.StringType):
				s = msg.decode(enc)
			else:
				s = unicode(msg,enc)
		except :
			continue
		return enc
	return None

いつぞやの Python-ML に流れていたコード。

5
3
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?