You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nbtlib创建含tag_List的NBT文件时报struct.error如何修复

错误原因

nbtlib 的 List 标签要求显式声明子元素的类型,你当前直接传入 map 生成的可迭代对象时,Python 3.8 环境下的 nbtlib 无法自动识别子元素为 nbt.Int 类型,导致写入文件时读取子标签 ID 失败,触发 struct.error 报错。

修复方案

修改 size 字段的构造逻辑,显式指定 List 的子元素类型为 nbt.Int,有两种写法可选:

写法1:泛型语法(推荐)

import nbtlib as nbt

class Structure(nbt.File):
    def __init__(self, data_version, size):
        super().__init__({
            "DataVersion": nbt.Int(data_version),
            "size": nbt.List[nbt.Int](map(nbt.Int, size))
        })

structure = Structure(data_version=1952, size=(0, 0, 0))
structure.save("prueba.nbt")

写法2:显式传subtype参数(兼容旧版本写法)

如果泛型语法在你的环境存在兼容问题,可以把size字段的构造改成如下形式:

"size": nbt.List(map(nbt.Int, size), subtype=nbt.Int)

修改后保存的NBT文件会完全符合你的结构要求,不会再出现写入报错。

内容的提问来源于stack exchange,提问作者Dante S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 00:18:04