Python | bytes 与 str 的区别

本文主要介绍在 Pythonbytesstr 的区别。1
Updated: 2022 / 6 / 16


bytes 与 str 的区别

1. Python 有两种类型可以表示字符序列

  • bytes
    下面所示实例包含的是原始数据 ,即 8 位的无符号值(通常按照 ASCII编码 标准来显示)
  • str
    下面所示实例包含的是 Unicode 码点(code point,也叫作代码点),这些码点与人类语言之中的文本字符相对应
a = b'h\x6511o'
print(list(a))
# [104, 101, 49, 49, 111]
print(a)
# b'he11o' 

a = 'a\\u300 propos'
print(list(a))
# ['a', '\\', 'u', '3', '0', '0', ' ', 'p', 'r', 'o', 'p', 'o', 's']
print(a)
# a\u300 propos

2. Unicode 数据和二进制数据转换

  • Unicode 数据转换成二进制数据,必须调用 strencode 方法(编码)
FileContent = 'This is file content.'
print(FileContent)
# 'This is file content.'
print(type(FileContent))
# <class 'str'>

FileContent = FileContent.encode(encoding='utf-8')
print(FileContent)
# b'This is file content.'
print(type(FileContent))
# <class 'bytes'>
  • 把二进制数据转换成 Unicode 数据,必须调用 bytesdecode 方法(解码)
FileContent = b'This is file content.'
print(FileContent)
# b'This is file content.'
print(type(FileContent))
# <class 'bytes'>

FileContent = FileContent.decode(encoding='utf-8')
print(FileContent)
# 'This is file content.'
print(type(FileContent))
# <class 'str'>

调用这些方法时,可以明确指出字符集编码,也可以采用系统默认的方案,通常是 UTF-8

当前操作系统默认的字符集编码,Python 一行代码查看当前操作系统默认的编码标准: 在 cmd 中执行:

python3 -c 'import locale; print(locale.getpreferredencoding())'
# UTF-8

3. 使用原始的 8 位值与 Unicode 字符串
使用原始的 8 位值与 Unicode 字符串时需要注意的两个问题 (该问题等价于使用 bytesstr 时需要注意的两个问题):

  • 3.1 bytesstr 的互不兼容

使用 + 操作符

# bytes+bytes
print(b'a' + b'1')
# b'a1'

# str+str
print('b' + '2')
# b2

# bytes+str
print('c' + b'2')
# TypeError: can only concatenate str (not "bytes") to str

同类型之间也可以用二元操作符来比较大小

# bytes bytes
assert b'c' > b'a'
assert b'c' < b'a'
# AssertionError
print(b'a' == b'a')
# True

# str str
assert 'c' > 'a'
assert 'c' < 'a'
# AssertionError
print('a' == 'a')
# True

# bytes str
assert b'c' > 'a'
# TypeError: '>' not supported between instances of 'bytes' and 'str'
print('a' == b'a')
# False

格式化字符串中的 %s

两种类型的实例都可以出现在 % 操作符的右侧,用来替换左侧那个格式字符串(format string)里面的 %s。但是如果格式字符串是 bytes 类型,那么不能用 str 实例来替换其中的 %s,因为 Python 不知道这个 str 应该按照什么字符集来编码。

# bytes % str
print(b'red %s' % 'blue'
# TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str'

# str % bytes 
print('red %s' % b'blue')
# red b'blue'
# @ 这样会让系统在 bytes 实例上面调用 __repr__ 方法。调用结果替换格式字符串里的 %s,因此程序会直接输出 b'blue',而不是输出 blue
  • 3.2 操作文件句柄时需要使用 Unicode 字符串操作, 不能使用原始的 bytes

w 模式必须以 ‘文本’ 模式写入, 否则向文件写入二进制数据会报错:

# 写入二进制数据
with open('test.txt', "w+") as f:
 f.write(b"\xf1\xf2")
# TypeError: write() argument must be str, not bytes

wb 可正常写入二进制数据

# 写入二进制数据
with open('test.txt', "wb") as f:
 f.write(b"\xf1\xf2")

r 模式必须以 ‘文本’ 模式写入, 否则从文件读取二进制数据会报错:

# 读取二进制数据
with open('test.txt', "r+") as f:
 f.read()
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 0: invalid continuation byte
# @以文本模式操纵文件句柄时,系统会采用 默认的文本编码 方案处理二进制数据。所以,上面那种写法会让系统通过 `bytes.decode` 把这份数据解码成 `str` 字符串,再用 `str.encode` 把字符串编码成二进制值。然而对于大多数系统来说,默认的文本编码方案是 `UTF-8`,所以系统很可能会把 `b'\xf1\xf2\xf3\xf4\xf5'` 当成 `UTF-8` 格式的字符串去解码,于是就会出现上面那样的错误。

rb 可正常读取二进制数据

# 写入二进制数据
with open('test.txt', "rb") as f:
 print(b"\xf1\xf2" == f.read())
# True

另一种改法,设置 encoding 参数指定字符串编码:

with open('test.txt', "r", encoding="cp1252") as f:
 print(f.read())


补充说明:






参考链接


  1. Python bytes 与 str 的区别 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值