Python之路,第十六篇:Python入门与基础16

python3   bytes 和 bytearrary

bytes

二进制文件的读写:

什么是二进制文件读:

文件中以字节(byte)为单位存储,不以换行符(\n)为单位分隔内容的文件;

1 f = open('mytext.txt', 'w')
2 f.write("你好中国")
3 f.close()
4 
5 $xxd   mytest.txt
6 e4db a0e5 a5bd b8ad e59b bd0a
View Code

二进制文件操作方法:

F.read( size = -1 )     从一个文件流中最多读出size个字符

F.write(字符串/字节串)    写一些数据到文件流中,返回写入的字节数(字符数)

F.tell()   返回当前文件流的绝对位置

F.seek( cookie,   whence=0)  改变数据流的位置,返回新的绝对位置

F.readable()           判断这个文件是否可读,可读则返回True

F.writable()            判断这个文件是否可写,可写则返回True

1 f = open('mytext.txt', 'rb')
2 b = f.read()  # bytes 类型
3 f.close()
4 print(len(b))  #用len取值
View Code

F.read()   返回类型: 

文本文件, 返回字符串

二进制文件,返回字节串(字节序列)

1 f = open('mytext.txt', 'wb')
2 f.write("你好".encode("utf-8"))    #6个字节
3 f.write(b"ABCD")
4 f.write(b"\n")
5 f.write(b"hello abcd\n")
6 f.close()
View Code
1 f = open('mytell.bin', 'wb')
2 print("f.tell返回:", f.tell())   #0
3 b = f.read(5)
4 print(b)
5 print("f.tell返回:", f.tell())   #5
6 f.close()
View Code

F.write(x) 函数

F.seek(偏移量, whence=相对位置)

偏移量: 大于0 的数代表向文件尾放心移动

                小于0 的代表向文件头方向

相对位置:

  0    代表从文件头开始偏移;

  1    代表从当前位置开始偏移;

  2     代表从文件尾开始偏移;

 1 f = open('mytell.bin', 'wb')
 2 print("f.tell返回:", f.tell())   #0
 3 b = f.read(5)
 4 print(b)
 5 print("f.tell返回:", f.tell())   #5
 6 
 7 f.seek(10, 0)  # 从开头向后10个字节
 8 f.seek(5, 1)   # 从当前位置向后移5个字节
 9 f.seek(-10, 2) # 从文件尾向前移动10个字节
10 b = f.read(5)  #读5个字节
11 print(b)   #b'12345'
12 
13 f.close()
View Code

bytearray类型

bytes 类型不可变类型
bytearray 类型为可变的数据类型


操作
+ += * *=
比较运算:< 、<= > >= == !=
in / not in 运算符
bytearray 的方法
B.clear() 清空
B.append(n) 追加一个字节(n为0-255的整数)
B.remove(value) 删除第一次出现的字节,如果没有出现,则产生ValueERROR错误
B.reverse() 字节的顺序进行反转
B.decode(encoding='utf-8') 转换成字符串
B.find(sub[, start[,end]]) 查找到了会返回大于等于零的数,查找失败返回-1的数;

创建字节串(bytearray)的函数
bytearray()创建空的字节串
bytearray (整数)
bytearray(整型可迭代对象)
bytearray(字符串,encoding='utf-8')
index /slice  索引/切片

 1 bytearray()
 2 bytearray(b'')
 3 bytearray(10)
 4 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
 5 bytearray([65,66,67,8])
 6 bytearray(b'ABC\x08')
 7 bytearray([65,66,67,68])
 8 bytearray(b'ABCD')
 9 bytearray('ABCD你好','utf-8')
10 bytearray(b'ABCD\xe4\xbd\xa0\xe5\xa5\xbd')
11 bytearray(b'abcdef')
12 bytearray(b'abcdef')
13 ba1 = bytearray(b'ABCD')
14 ba2 = bytearray(b'EF')
15 ba1 + ba2
16 bytearray(b'ABCDEF')
17 ba1
18 bytearray(b'ABCD')
19 ba1[0]
20 65
21 ba1[0]=97
22 ba1
23 bytearray(b'aBCD')
24 ba3 = bytearray(b'ABCDEF')
25 ba3[::2]
26 bytearray(b'ACE')
27 ba3
28 bytearray(b'ABCDEF')
29 ba3.clear()
30 ba3
31 bytearray(b'')
32 ba3.append(97)
33 ba3
34 bytearray(b'a')
35 ba3.append(98)
36 ba3
37 bytearray(b'ab')
38 ba3.append(100)
39 ba3.append(97)
40 ba3
41 bytearray(b'abda')
42 ba3.remove(97)
43 ba3
44 bytearray(b'bda')
45 ba3.remove(97)
46 ba3
47 bytearray(b'bd')
48 ba3.remove(97)
49 Traceback (most recent call last):
50   File "<input>", line 1, in <module>
51 ValueError: value not found in bytearray
52 ba = bytearray(b'A1B2C3D4')
53 ba.reverse()
54 ba
55 bytearray(b'4D3C2B1A')
56 s = ba.decode('utf-8')
57 s
58 '4D3C2B1A'
59 type(s)
60 <class 'str'>
61 ba
62 bytearray(b'4D3C2B1A')
63 ba.find(bytearray(b'3C'))
64 2
65 ba.find(bytearray(b'3CC'))
66 -1
View Code

猜你喜欢

转载自www.cnblogs.com/weizitianming/p/9074122.html
今日推荐