zoukankan      html  css  js  c++  java
  • Python UNICODE GBK UTF-8 之间相互转换

    Python 编码格式检测,可以使用 chardet ,

    例如: 

        

    import urllib
    rawdata = urllib.urlopen('http://www.google.cn/').read()
    import chardet
    print chardet.detect(rawdata)
    
    输出结果是:
        {'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
    # win下命令行参数为gbk编码:star.gbk2unicode(sys.argv[1]) + u'也有'
    def gbk2unicode(s):
        return s.decode('gbk', 'ignore')
    
    # 脚本文件#coding:utf-8时默认不带u的字符串为utf8字符串:star.utf82unicode('我')
    def utf82unicode(s):
        return s.decode('utf-8', 'ignore')
    
    # 带u的字符串为unicode
    # star.unicode2gbk(u'u4e5fu6709')
    # star.unicode2gbk(u'也有')
    def unicode2gbk(s):
        return s.encode('gbk')
    
    # 带u的字符串为unicode
    # star.unicode2utf8(u'u4e5fu6709')
    # star.unicode2utf8(u'也有')
    def unicode2utf8(s):
        return s.encode('utf-8')
    
    # win下命令行参数为gbk编码:star.gbk2utf8(sys.argv[1]) + '也有'
    def gbk2utf8(s):
        return s.decode('gbk', 'ignore').encode('utf-8')
    
    def utf82gbk(s):
        return s.decode('utf-8', 'ignore').encode('gbk')
  • 相关阅读:
    物理-接触力:百科
    物理-二力平衡:百科
    物理-摩擦力:百科
    物理-电磁力/静电力:百科
    物理-重力:百科
    化学-分子间作用力:百科
    物理-分子力:百科
    物理-斥力:百科
    物理-粒子/能量-衰变:百科
    物理-超光速:百科
  • 原文地址:https://www.cnblogs.com/xuchunlin/p/7253954.html
Copyright © 2011-2022 走看看