zoukankan      html  css  js  c++  java
  • Python

    # coding: utf-8
    
    s = 'abc'
    print type(s) # str(utf-8)
    print len(s) # 3
    
    s = unicode(s) # str -> unicode,其中str的每个字符值必须小于128
    print type(s) # unicode
    print len(s) # 3
    
    s = u'abc'
    print type(s) # unicode
    print len(s) # 3
    
    s = s.encode('utf-8') # unicode -> str(utf-8)
    print type(s) # str
    print len(s) # 3
    
    s = s.decode('utf-8') # str(utf-8) -> unicode,这里str的每个字符值任意
    print type(s) # unicode
    print len(s) # 3
    
    s = '中国' # 由于整个文件以utf-8编码
    print type(s) # str(utf-8)
    print len(s) # 6
    
    s = u'中国'
    print type(s) # unicode
    print len(s) # 2
    
    s = s.encode('utf-8')
    print type(s) # str(utf-8)
    print len(s) # 6
    
    s = s.decode('utf-8')
    print type(s) # unicode
    print len(s) # 2
    
    s = raw_input(u'输入:') # windows下貌似中文按gbk编码,每个中文占2个字节
    print type(s) # str(gbk)
    print len(s) # 4
    
    s = s.decode('gbk') # 要想gbk编码转为utf-8编码,先将gbk编码转为unicode
    print type(s) # unicode
    print len(s) # 2
    
    s = s.encode('gbk')
    print type(s) # str(gbk)
    print len(s) # 4
    
    # 根据以上的验证,得出结论
    # 各种编码都可以通过unicode来转化,unicode可以假想为一张各种字符的对照表,在这个表中可以找到世界范围内的任何一种字符
    # 当然,也包括中文,每个字符都对应一个序号,如'a' -> 0x61,'中' -> 0x4e2d
    # unicode -> utf-8 unicode.encode('utf-8')
    # utf-8 -> unicode str.decode('utf-8')
    # gbk -> unicode str.decode('gbk')
    # unicode -> gbk unicode.encode('gbk')
  • 相关阅读:
    JMETER(七) 后置处理器
    JMETER(六) 前置处理器
    JMETER(四) 配置元件
    QNET网络测试工具
    修改exe程序图标
    自解压捆绑文件钓鱼
    BigIP Cookie 解码获取真实IP (python2)
    关于windowspwnsafeseh的理解简单随笔
    bilibili动画下载视频批量改名(python)
    好用!网页模板网站
  • 原文地址:https://www.cnblogs.com/juanbai/p/6508110.html
Copyright © 2011-2022 走看看