python3.x的Bytes对象

1 python3.x的Bytes对象

python3.x的bytes对象是较小整数的一个序列,属于字节序列,属于字节串,和python2.x的str对应,每个整数对应一个字节,而每个字节为8位,所以每个整数都在[0,255]之间。

用2个16进制表示1个字节,以b或B开头,比如,b’\xe6\xa2\xaf’。

bytes对象不支持格式化方法和%格式化表达式。

1.1 方法调用

bytes对象支持str对象的大多数方法。

>>> strf,bytesf=set(dir('a')),set(dir(b'a'))
# str对象和bytes对象都有的属性方法
>>> strf&bytesf
{'__add__', '__new__', 'partition', '__repr__', '__mod__', '__format__', 'isdigit', 'isalpha', '__class__', 'center', '__eq__', '__mul__', '__ge__', 'ljust', 'swapcase', '__getitem__', 'lower', 'isalnum', 'lstrip', 'isupper', 'maketrans', 'strip', 'index', 'find', 'rsplit', 'startswith', '__setattr__', 'expandtabs', 'isspace', '__gt__', '__str__', 'count', '__subclasshook__', '__getattribute__', 'isascii', 'join', '__lt__', 'rindex', 'endswith', 'title', '__ne__', '__hash__', '__rmul__', '__init_subclass__', '__iter__', 'capitalize', '__dir__', '__delattr__', 'upper', 'replace', '__reduce_ex__', '__rmod__', 'rpartition', 'translate', '__contains__', '__getnewargs__', 'rfind', '__init__', '__len__', 'rstrip', 'rjust', '__le__', 'istitle', 'islower', 'splitlines', '__reduce__', 'split', 'zfill', '__sizeof__', '__doc__'}
# str对象有bytes对象没有的属性方法
>>> strf-bytesf
{'format', 'encode', 'isprintable', 'format_map', 'casefold', 'isidentifier', 'isdecimal', 'isnumeric'}
# bytes对象有str对象没有的属性放
>>> bytesf-strf
{'hex', 'decode', 'fromhex'}

1.2 创建bytes对象

1.2.1 通过字面值创建

>>> b=b'python'
>>> b,type(b)
(b'python', <class 'bytes'>)
# 字面值只能创建ASCII字符
>>> b'梯'
SyntaxError: bytes can only contain ASCII literal characters.

1.2.2 通过encode()创建

>>> b='python'.encode()
>>> b,type(b)
(b'python', <class 'bytes'>)

1.2.3 通过bytes()创建

# 通过 bytes(整数序列)创建,范围必须是[0,255]
>>> b=bytes((1,3,5,7,9))
>>> b,type(b)
(b'\x01\x03\x05\x07\t', <class 'bytes'>)
>>> bytes((0,255))
b'\x00\xff'
>>> bytes((-1,256))
Traceback (most recent call last):
  File "<pyshell#60>", line 1, in <module>
    bytes((-1,256))
ValueError: bytes must be in range(0, 256)
# 通过 bytes(字符串,encoding) 创建 字符串对应的原始字节序列bytes
>>> b=bytes('梯','utf-8')
>>> b,type(b)
(b'\xe6\xa2\xaf', <class 'bytes'>)
# 通过 bytes(整数n) 创建指定个数n个,内容为0的字节序列
>>> b=bytes(2)
>>> b,type(b)
(b'\x00\x00', <class 'bytes'>)

1.3 len(bytes对象)

# len(bytes对象)获取bytes对象长度
>>> len('梯'.encode('utf-8'))
3
>>> len(bytes(2))
2

1.4 访问bytes对象

描述

同字符串的访问方式一样访问bytes对象。比如用索引访问。

示例

>>> b_utf8='梯'.encode('utf-8')
>>> b_gbk='梯'.encode('gbk')
# 1个字节算1个长度
>>> tuple(map(len,(b_utf8,b_gbk)))
(3, 2)
# 1个字节算1个元素,值为整数,范围[0,255]
>>> tuple(map((lambda seq:tuple(i for i in seq)),(b_utf8,b_gbk)))
((230, 162, 175), (204, 221))
# 通过索引访问 bytes 对象
>>> tuple(map((lambda seq:tuple(seq[i] for i in range(len(seq)))),(b_utf8,b_gbk)))
((230, 162, 175), (204, 221))
>>> b_utf8[0]
230
>>> b_utf8[-1]
175
>>> b_utf8[:]
b'\xe6\xa2\xaf'
>>> b_utf8[1:]
b'\xa2\xaf'

1.5 不可修改bytes对象

# bytes对象不支持修改
>>> b_utf8[0]=2
Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    b_utf8[0]=2
TypeError: 'bytes' object does not support item assignment

1.6 比较bytes对象

# 比较bytes对象的字节值
>>> b_utf8==b_gbk
False

1.7 bytes对象的+和*

# bytes对象的+(连接)、*(重复)操作与字符串str一致
>>> b_gbk+b_utf8
b'\xcc\xdd\xe6\xa2\xaf'
>>> b_gbk*2
b'\xcc\xdd\xcc\xdd'
# 字节串 bytes , 不能和字符串 str 连接
>>> b_gbk+'梯'
Traceback (most recent call last):
  File "<pyshell#92>", line 1, in <module>
    b_gbk+'梯'
TypeError: can't concat str to bytes
# 显式转换后可以通过+连接
>>> b_gbk+'梯'.encode('utf-8')
b'\xcc\xdd\xe6\xa2\xaf'

1.8 bytes对象的replace()

>>> b_gbk
b'\xcc\xdd'
# replace 不会原地修改 bytes 对象
>>> b_gbk.replace(b'\xcc',b'\xaa')
b'\xaa\xdd'
>>> b_gbk
b'\xcc\xdd'
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值