PythonTip >> 博文 >> python

python - summary with code on Python Bytes Esse...

zihua 2014-01-16 18:01:09 点击: 784 | 收藏


Bytes are often thought as sequence of raw bytes. how a sequence of bytes is interpreted is determined by the encode system. 

the string.encode method will convert the string to bytes method, and the decode method will convert the bytes object to string object.  you may be able to specify different code system.

the following method will be discussed

  • encode
  • decode

please find the code below. 

'''
Created on 2012-10-31

@author: Administrator
file: ByteEssentials.py
description: ByteEssentials.py  demonstrate hte use of 
   encode
   decode
'''

def byte_basics():
    unicode_a_with_acute = '\N{LATIN SMALL LETTER A WITH ACUTE}'
    print(unicode_a_with_acute)
    xb = unicode_a_with_acute.encode()
    print(xb)
    try:
        xb += 'A'
    except:
        print("can't concat bytes to str")
        pass
    converted_back = xb.decode()
    print(converted_back)


if __name__ == '__main__':
    byte_basics()

原文链接:http://my.oschina.net/u/854138/blog/86382

作者:zihua | 分类: python | 标签: python | 阅读: 784 | 发布于: 2014-01-16 18时 |