<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      python技巧31[unicode和bytes]

       

      一 Python3 中字符串的類(lèi)型

       

      bytearray([source[, encoding[, errors]]])

      Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256.

      bytes([source[, encoding[, errors]]])

      Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256. bytes is an immutable version of bytearray.

      str([object[, encoding[, errors]]])

      Return a string version of an object. str默認(rèn)為unicode的字符串。

       

       

      貌似也沒(méi)有了2.x中的basestring類(lèi)型了。

       

      二 實(shí)例

       

      # -*- coding: gbk -*-

      def TestisStrOrUnicdeOrString():
        bs 
      = b'Hello'
        ustr 
      = 'abc'
        
      print (isinstance(bs, str))  #False
        print (isinstance(bs,bytes)) #True
        print (isinstance(ustr,str)) #True
        print (isinstance(ustr, bytes)) #False
        print (isinstance(bs,(bytes,str))) #True


      def TestChinese():
        us 
      = '中國(guó)'
        bs 
      = b'AAA'
        bs2 
      = bytes('中國(guó)','gbk')
        
        
      print (us + ':' + str(type(us))) #中國(guó):<class 'str'>
        print (bs) #b'AAA'
        print (bs2) # b'\xd6\xd0\xb9\xfa'
        print (':' + str(type(bs2))) #:<class 'bytes'>
        print (bs2.decode('gbk')) #中國(guó)
        
        
      # TypeError: Can't convert 'bytes' object to str implicitly
        #newstr = us + bs2
        
        
      print ('us == bs2' + ':' + str(us == bs2)) #us == bs2:False
        
        s3 
      = 'AAA中國(guó)'
        
      print (s3) # AAA中國(guó)
        
        s4 
      = bytes('AAA中國(guó)','gbk')
        
      print (s4) # b'AAA\xd6\xd0\xb9\xfa'
        
      def TestPrint():
        
      print ('AAA' + '中國(guó)')  # AAA中國(guó)
        #print (b'AAA' + b'中國(guó)') #  # SyntaxError: bytes can only contain ASCII literal characters.
        #print ('AAA' + bytes('中國(guó)','gbk')) # TypeError: Can't convert 'bytes' object to str implicitly
        
      def TestCodecs():
          
      import codecs
          
          look  
      = codecs.lookup("gbk")

          a 
      = bytes("北京",'gbk')

          
      print (len(a), a, type(a)) #4 b'\xb1\xb1\xbe\xa9' <class 'bytes'>

          b 
      = look.decode(a)
          
      print (b[1], b[0], type(b[0])) #4 北京 <class 'str'>
          

      if __name__ == '__main__':
          TestisStrOrUnicdeOrString()
          TestChinese()
          TestPrint()
          TestCodecs()

       

       

      三 總結(jié)

       

      1) Python 3會(huì)假定我們的源碼 — 即.py文件 — 使用的是UTF-8編碼方式。Python 2里,.py文件默認(rèn)的編碼方式為ASCII。可以使用# -*- coding: windows-1252 -*-方式來(lái)改變文件的編碼。如果py文件中包含中文的字符串,則需要制定為# -*- coding: gbk -*-,貌似默認(rèn)的utf8不夠哦。

      2) python3中默認(rèn)的str為unicode的,可以使用str.encode來(lái)轉(zhuǎn)為bytes類(lèi)型。

      3) python3的print函數(shù)只支持unicode的str,貌似沒(méi)有對(duì)bytes的解碼功能,所以對(duì)對(duì)不能解碼的bytes不能正確輸出。

      4) str和bytes不能連接和比較。

      5) codecs任然可以用來(lái)str和bytes間的轉(zhuǎn)化。

      6) 定義非ascii碼的bytes時(shí),必須使用如 bytes('中國(guó)','gbk') 來(lái)轉(zhuǎn)碼。

       

      7)貌似必須在中文系統(tǒng)或者系統(tǒng)安裝中文的語(yǔ)言包后gbk解碼才能正常工作。

       

      python 2.6 的字符及編碼轉(zhuǎn)化見(jiàn) :http://www.rzrgm.cn/itech/archive/2011/03/27/1996883.html

       

       

       

      完!

      posted @ 2011-03-28 17:38  iTech  閱讀(18581)  評(píng)論(1)    收藏  舉報(bào)
      主站蜘蛛池模板: 色五月丁香六月欧美综合| 亚洲国产精品成人综合色在| 成人无码精品1区2区3区免费看| 国产在线线精品宅男网址| 一本本月无码-| 天堂资源国产老熟女在线| 色国产视频| 国产精品亚洲一区二区z| 九九热在线免费观看视频| 中文无码vr最新无码av专区| 亚洲人成网网址在线看| 懂色AV| 视频一区二区三区高清在线| 99热这里有精品| 色综合五月伊人六月丁香| 金坛市| 久青草国产在视频在线观看| 午夜天堂一区人妻| 精品视频一区二区福利午夜| 亚洲人成网站77777在线观看| 无码成人精品区在线观看| 大尺度国产一区二区视频| 国产av永久无码天堂影院| 免费看婬乱a欧美大片| 国产av一区二区三区久久| 亚洲人成网站在线无码| 中文字幕国产精品综合| 久久久无码人妻精品无码| 水蜜桃av无码| 99久久精品费精品国产一区二| AV最新高清无码专区| 亚洲丶国产丶欧美一区二区三区| 亚洲天堂激情av在线| 国产精品久久久久免费观看| 99国产精品白浆无码流出| 高清破外女出血AV毛片| 99久久久国产精品免费无卡顿| 老熟妇乱子交视频一区| 国产成人精彩在线视频| 无码中文字幕日韩专区| 国产精品任我爽爆在线播放6080|