脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python如何读写字节数据

Python如何读写字节数据

2020-08-05 15:52David Beazley Python

这篇文章主要介绍了Python如何读写字节数据,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下

问题

你想读写二进制文件,比如图片,声音文件等等。

解决方案

使用模式为 rb 或 wb 的 open() 函数来读取或写入二进制数据。比如:

?
1
2
3
4
5
6
7
# Read the entire file as a single byte string
with open('somefile.bin', 'rb') as f:
  data = f.read()
 
# Write binary data to a file
with open('somefile.bin', 'wb') as f:
  f.write(b'Hello World')

在读取二进制数据时,需要指明的是所有返回的数据都是字节字符串格式的,而不是文本字符串。 类似的,在写入的时候,必须保证参数是以字节形式对外暴露数据的对象(比如字节字符串,字节数组对象等)。

讨论

在读取二进制数据的时候,字节字符串和文本字符串的语义差异可能会导致一个潜在的陷阱。 特别需要注意的是,索引和迭代动作返回的是字节的值而不是字节字符串。比如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> # Text string
>>> t = 'Hello World'
>>> t[0]
'H'
>>> for c in t:
...   print(c)
...
H
e
l
l
o
...
>>> # Byte string
>>> b = b'Hello World'
>>> b[0]
72
>>> for c in b:
...   print(c)
...
72
101
108
108
111
...
>>>

如果你想从二进制模式的文件中读取或写入文本数据,必须确保要进行解码和编码操作。比如:

?
1
2
3
4
5
6
7
with open('somefile.bin', 'rb') as f:
  data = f.read(16)
  text = data.decode('utf-8')
 
with open('somefile.bin', 'wb') as f:
  text = 'Hello World'
  f.write(text.encode('utf-8'))

二进制I/O还有一个鲜为人知的特性就是数组和C结构体类型能直接被写入,而不需要中间转换为自己对象。比如:

?
1
2
3
4
import array
nums = array.array('i', [1, 2, 3, 4])
with open('data.bin','wb') as f:
  f.write(nums)

这个适用于任何实现了被称之为”缓冲接口”的对象,这种对象会直接暴露其底层的内存缓冲区给能处理它的操作。 二进制数据的写入就是这类操作之一。

很多对象还允许通过使用文件对象的 readinto() 方法直接读取二进制数据到其底层的内存中去。比如:

?
1
2
3
4
5
6
7
8
9
>>> import array
>>> a = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0])
>>> with open('data.bin', 'rb') as f:
...   f.readinto(a)
...
16
>>> a
array('i', [1, 2, 3, 4, 0, 0, 0, 0])
>>>

但是使用这种技术的时候需要格外小心,因为它通常具有平台相关性,并且可能会依赖字长和字节顺序(高位优先和低位优先)。

以上就是Python如何读写字节数据的详细内容,更多关于Python读写字节数据的资料请关注服务器之家其它相关文章!

原文链接:https://python3-cookbook.readthedocs.io/zh_CN/latest/c05/p04_read_write_binary_data.html

延伸 · 阅读

精彩推荐