泡菜转储方法添加Wierd二进制数据

发布于 2025-02-13 13:36:43 字数 601 浏览 0 评论 0原文

每个人!

我在文件中使用pickle.dump()编写一些二进制数据,然后读取以下代码的文件。

import numpy as np
import pickle

save_file = open('test.rdb','wb')
save_data = np.array([1, 2, 3])
pickle.dump(np.ndarray.tobytes(save_data),save_file)
save_data = np.array([1.0, 2.0, 3.0])

pickle.dump(np.ndarray.tobytes(save_data),save_file)
save_file.close()

read_file = open('test.rdb','rb')
data = read_file.read()
print(data)

当我使用十六进制编辑器打开文件时,有一些我没有编写的数据。 (下图中的红色框)

二进制文件图片在这里

这些是什么?我该如何删除它们?

everyone!

I write some binary data using pickle.dump() in a file, and read the file as below code.

import numpy as np
import pickle

save_file = open('test.rdb','wb')
save_data = np.array([1, 2, 3])
pickle.dump(np.ndarray.tobytes(save_data),save_file)
save_data = np.array([1.0, 2.0, 3.0])

pickle.dump(np.ndarray.tobytes(save_data),save_file)
save_file.close()

read_file = open('test.rdb','rb')
data = read_file.read()
print(data)

When I open the file using hex editor, there are some data that I didn't write.
(Red box in the picture below)

Binary file picture is here

What are those? and how can I remove them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-20 13:36:43

我建议不要使用Pickle来转储文件,而请使用Python默认打开函数在WB中打开文件,然后转储文件。倾倒时,它不会添加任何额外的二进制字节。

binary_file = open('test.rdb', "wb")
binary_file.write(np.ndarray.tobytes(save_data))
binary_file.close()

I suggest instead of using pickle to dump file, use python default open function to open file in wb and then dump the file. It will not add any extra binary bytes when dumping.

binary_file = open('test.rdb', "wb")
binary_file.write(np.ndarray.tobytes(save_data))
binary_file.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文