# Encoding:utf-8
‘‘‘
Created on February 8, 2015
@author: Zhang Peng Cheng [email protected]
@copyright: All rights reserved, respect labor success, reprint and amend please indicate the author
‘‘‘
Import Traceback
Import Chardet
def Mytoutf8 (s):
return Mytounicode (s). Encode (' Utf-8 ')
def mytounicode (s):
If type (s) = = Type (U '):
# print ' 1 '
return s
Try
# print ' 2 '
s = S.decode (' Utf-8 ')
Except
Try
# print ' 3 '
s = S.decode (' GB18030 ')
Except
print ' ***error:decode string ({0}) '. Format (repr (s))
Print Traceback.print_exc ()
s = repr (s)
# print ' 4 '
return s
if __name__ = = ' __main__ ':
# Test China I Love you
# Utf-8
s = ur ' China i love you '
Print repr (s), S
CC = [' Utf-8 ', ' gb18030 ', ' GBK ']
fn = [Mytounicode, Mytoutf8,]
For F in FN:
For C in cc:
# print ' = ' * 80
print ' {0:<20} ({1:10}) = {2:<50}, {3} '. Format (f.__name__, C, repr (f (S.encode (c))), F (S.encode (c)))
Python arbitrary encoding to UTF8 or Unicode