码迷,mamicode.com
首页 > 编程语言 > 详细

python任意编码转utf8或者unicode

时间:2015-02-09 22:53:18      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

# encoding: utf-8
‘‘‘
Created on 2015年2月8日

@author: 张鹏程 aprial@163.com
@copyright: 版权所有, 尊重劳动成功, 转载与修改请注明作者
‘‘‘
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 中国i love you
    # utf-8
    s = ur‘中国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任意编码转utf8或者unicode

标签:

原文地址:http://www.cnblogs.com/zhang-pengcheng/p/4282513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!