python - 如何使用 numpy 从二进制文件中获取字节数据
问题描述
我必须从文件中获取一些字节作为数据。但是要读取的字节数会根据某些字节而有所不同。这是一个例子。数据格式为 128 字节。前 64 个字节类似。所以没问题。但接下来的 64 个字节是可变的。
Let us say 65th byte is "a", then
66 + 67 bytes = var x
68+69+70+71 bytes = var y
In case 65th byte is "b" , then
66th byte : var z
67th byte: var k
68+69 =var o
70+71+72+73 = var m
dtype = [('var1',np.uint32),('var2',np.uint16).......]
with open(filename,'rb') as fh:
data = np.fromfile(fh,dtype)
是否可以使用 numpy 和 dtype 获取此类数据。还是我需要运行一个 for 循环?循环会很昂贵,因为这是一个巨大的文件。有什么想法可以解决吗?
解决方案
推荐阅读
- node.js - tabris.js websocket只能接收字符串
- react-native - 从选项卡导航选项卡打开抽屉导航
- python - 如何使用 python 提交对 google-forms 的响应?
- azure - 如何让 Azure 应用服务信任 OnPrem CA 颁发的证书?
- javascript - useMutation 总是返回未定义的数据
- ruby - 使用 VScode 在 Ruby 中显示 API 调用的 JSON 输出
- go - 磁盘使用信息
- java - 如何在 2019 年从 Java 链接到“unix-java”
- spring-boot - 运行 Junit 时未找到 Lombok 日志变量
- javascript - 如何通过 ReactJS 中的嵌套对象进行映射并显示所有键和值