Skip to content

Instantly share code, notes, and snippets.

@mozillazg
Created October 11, 2012 05:57
Show Gist options
  • Save mozillazg/3870483 to your computer and use it in GitHub Desktop.
Save mozillazg/3870483 to your computer and use it in GitHub Desktop.
简单猜测字符串编码并返回 Unicode 字符串
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""简单猜测字符串编码并返回 Unicode 字符串
"""
def decode_(str_):
"""
"""
text = str_
charests = ('utf8', 'gbk', 'gb2312', 'big5', 'ascii',
'shift_jis', 'euc_jp', 'euc_kr', 'iso2022_kr',
'latin1', 'latin2', 'latin9', 'latin10', 'koi8_r',
'cyrillic', 'utf16', 'utf32'
)
if isinstance(text, unicode):
return text
else:
for i in charests:
try:
return text.decode(i)
break
except:
pass
else:
return None
if __name__ == '__main__':
text = 'abc你'
print repr(decode_(text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment