python - 优化仅在 Python 中搜索一小部分数据的 NBT 解析器
问题描述
nbt 文件由 Mincraft 的原理图模块创建。
有了content.find(f'\xYY\xZZ..')
并且content = content[lower:upper]
我能够将数据切片到最重要的部分:翻译时具有以下形式的块数据(我的数据未翻译!):
TAG_List("blocks (Compound)"): (xdim*ydim*zdim) entries of type TAG_Compound
{
TAG_Compound:
{
TAG_List("pos (Int)"): 3 entries of type TAG_Int
{
TAG_Int: X
TAG_Int: Y
TAG_Int: Z
}
TAG_Int("state"): S
}
//...
TAG_Compound:
{
TAG_Compound("nbt"):
{
//Some uninteresting stuff
}
TAG_List("pos (Int)"): 3 entries of type TAG_Int
{
TAG_Int: X
TAG_Int: Y
TAG_Int: Z
}
TAG_Int("state"): S
}
}
我的目标是尽可能快地提取原始字节数据的所有“状态”信息,同时忽略所有“pos”和“nbt”数据。由于不是每个“块”-化合物都有一个“nbt”-子化合物,我不能只保存每个 X 字节,因为每个“块”-化合物的长度是未知的。
所以我目前的解决方案是搜索关键字“state”(data.find(b'\x73\x74\x61\x74\x65')
),保存以下4个字节,从数据(data = data[index+4:]
)中剪切“state”这个词并迭代。
而且我绝不是专家,但我觉得必须有更好的方法。
为了让您更好地了解我正在处理的数据:
b'\t\x00\x06blocks\n\x00\x00\x00\x08\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x05state\x00\x00\x00\x00\x00\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x05state\x00\x00\x00\x01\x00\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x03\x00\x05state\x00\x00\x00\x02\x00\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x03\x00\x05state\x00\x00\x00\x03\x00\n\x00\x03nbt\t\x00\x05Items\x00\x00\x00\x00\x00\x08\x00\x02id\x00\x0fminecraft:chest\x08\x00\x04Lock\x00\x00\x00\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x00\x05state\x00\x00\x00\x04\x00\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x03\x00\x05state\x00\x00\x00\x05\x00\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x03\x00\x05state\x00\x00\x00\x03\x00\t\x00\x03pos\x03\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x00\x05state\x00\x00\x00\x03\x00'
(我想要每个“状态”之后的 4 个字节)
//编辑:我刚刚意识到切片需要很多时间,所以如果我只是搜索所有出现的情况,我会快很多。但我还是希望有更好的方法
解决方案
推荐阅读
- excel - VBA更改连接名称
- sql-server - 如何将 SQL SERVER 函数转换为 PROCEDURE
- android - 强制创建 Couchbase Lite 2.6 数据库
- dynamic - 使用 insertUI 的具有动态 bs_accordion_sidebar() 的闪亮应用
- xamarin - 使用 Refit 在 Xamarin 中请求开始失败
- java - 如何将jmeter中的参数作为变量发送?
- json - WildFly 中的 jackson-jaxrs 提供程序与 EAR 部署冲突
- python - Python双重列表理解
- python - Python。我可以通过 PyQt5 在 OpenCV 中使用 cv2.setMouseCallback() 吗?
- node.js - 当我在异步映射函数中创建数据库进程时,我无法避免重复