Bytes和Str的区别
在Python3中,字符序列有两种类型:bytes和str。bytes类型是无符号的8位值(通常以ASCII码显式),而str类型是Unicode代码点(code point)。代码点指编码字符集中,字符所对应的数字。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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)有着严格的区分,不能混用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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()方法。
1
2
3
4
5
6
7
8
9
|
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类型的字符序列了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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参数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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) # 涓栫晫浣犲ソ |
总结
到此这篇关于Python中bytes和str区别与联系的文章就介绍到这了,更多相关Python bytes和str区别内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_42103091/article/details/124573589