Python中bytes和str的区别与联系详解

作者:斯曦巍峨 时间:2022-04-18 18:36:57 

Bytes和Str的区别

在Python3中,字符序列有两种类型:bytes和str。bytes类型是无符号的8位值(通常以ASCII码显式),而str类型是Unicode代码点(code point)。代码点指编码字符集中,字符所对应的数字。

a = b'hello world'
print(isinstance(a, bytes))
print(list(a))
print(a)
"""
True
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
b'hello world'
"""

a = 'hello world'
print(isinstance(a, str))
print(list(a))
print(a)
"""
True
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
hello world
"""

isinstance()方法可以判断对象的类型,例如这里用来判断是str还是bytes。

Python3对文本(str)和二进制数据(bytes)有着严格的区分,不能混用。

x = b'python'
y = b'java'
z = 'c++'
w = 'c'

print(x + y)
# b'pythonjava'
print(z + w)
# c++c
print(x + z)
# TypeError: can't concat str to bytes

print('python' == b'python')
# False

上述示例中str类型和bytes类型间使用=来比较是否相等不会报错,但是会返回False。

Bytes与Str间的转换

str类型和bytes类型间可以相互转换。

str到bytes的转换需要调用encode()方法。

bytes到str间的转换需要调用decode()方法。

x = b'python'
y = x.decode(encoding='utf-8')
z = y.encode(encoding='utf-8')
print(y)
print(z)
"""
python
b'python'
"""

可以观察到encode()和decode()方法都有一个encoding参数用来指定具体的编码规则。

读写文件的注意事项

当要将bytes类型写入到文件中时,必须指定mode=wb。读取二进制文件时可以指定mode=rb或者指定编码方式,使用后者时读出来的就不是bytes类型的字符序列了。

x = b'python'

# 错误示例
with open('data.bin', mode='w') as fp:
   fp.write(x)
# TypeError: write() argument must be str, not bytes

# 正确示例
with open('data.bin', mode='wb') as fp:
   fp.write(x)

# 读取二进制文件方式1
with open('data.bin', mode='rb') as fp:
   content = fp.read()
   print(content)
# python

# 读取二进制文件方式2
with open('data.bin', mode='r', encoding='utf-8') as fp:
   content = fp.read()
   print(content, type(content))
# python <class 'str'>

当读写Unicode数据时,只需要注意下编码方式即可,最好是显式的传递encoding参数。

x = '世界你好'

with open('data.txt', mode='w', encoding='utf-8') as fp:
   fp.write(x)

with open('data.txt', mode='r', encoding='utf-8') as fp:
   content = fp.read()
   print(content)
# 世界你好

# 错误示例,编码方式不对
with open('data.txt', mode='r', encoding='gbk') as fp:
   content = fp.read()
   print(content)
# 涓栫晫浣犲ソ

来源:https://blog.csdn.net/qq_42103091/article/details/124573589

标签:python,bytes,str
0
投稿

猜你喜欢

  • 讲解数据库管理系统必须提供的基本服务

    2009-01-04 14:33:00
  • 从零开始搭建基于Python的微信小程序的教程分享

    2022-09-08 19:38:59
  • PHP date函数参数详解

    2023-11-23 06:41:40
  • python中内置函数ord()返回字符串的ASCII数值实例详解

    2023-09-05 07:07:26
  • Python os和os.path模块详情

    2022-12-08 12:53:45
  • 90行Python代码开发个人云盘应用

    2021-12-17 13:44:12
  • Pycharm最新激活码2019(推荐)

    2023-07-24 20:30:26
  • python通过BF算法实现关键词匹配的方法

    2022-10-19 03:56:37
  • Python lxml模块安装教程

    2021-08-26 22:23:43
  • python正则表达式最详解

    2022-03-13 07:29:47
  • python selenium 获取标签的属性值、内容、状态方法

    2021-03-12 23:02:46
  • 解读ASP.NET 5 & MVC6系列教程(12):基于Lamda表达式的强类型Routing实现

    2023-06-28 15:17:35
  • ACCESS如何打印窗体中当前显示的记录

    2008-11-20 16:31:00
  • 8个asp生成随机字符的函数

    2007-08-04 10:17:00
  • 通过排序引导用户的行为方式

    2008-05-17 09:30:00
  • torch 中各种图像格式转换的实现方法

    2021-04-17 08:08:13
  • 如何获知IE和NC客户端的屏幕分辨率?

    2009-11-23 20:56:00
  • PHP PDOStatement::fetchColumn讲解

    2023-06-06 09:17:20
  • Python + Tkinter连接本地MySQL数据库简单实现注册登录

    2023-04-24 22:07:07
  • Python实现双X轴双Y轴绘图的示例详解

    2021-10-17 17:33:58
  • asp之家 网络编程 m.aspxhome.com