首页 > 代码库 > bytes

bytes

class bytes(object):    """    bytes(iterable_of_ints) -> bytes    bytes(string, encoding[, errors]) -> bytes    bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer    bytes(int) -> bytes object of size given by the parameter initialized with null bytes    bytes() -> empty bytes object        Construct an immutable array of bytes from:      - an iterable yielding integers in range(256)      - a text string encoded using the specified encoding      - any object implementing the buffer API.      - an integer    """

Python中的字节码用b‘xxx‘的形式表示。x可以用字符表示,也可以用ASCII编码形式\xnn表示,nn从00-ff(十六进制)共256种字符。 

a = ‘abc‘b = bytes(a, ‘utf-8‘)print(b)c = bytes(a, ‘gbk‘)print(c)# 结果 b‘abc‘# 结果 b‘abc‘a = ‘你好‘b = bytes(a, ‘utf-8‘)print(b)c = bytes(a, ‘gbk‘)print(c)# 结果 b‘\xe4\xbd\xa0\xe5\xa5\xbd‘# 结果 b‘\xc4\xe3\xba\xc3‘

decode

c = bytes(a, ‘gbk‘).decode(‘gbk‘)print(c)# 结果 你好

  

bytes