Created
October 11, 2012 05:57
-
-
Save mozillazg/3870483 to your computer and use it in GitHub Desktop.
简单猜测字符串编码并返回 Unicode 字符串
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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