命名二进制标记读取器/写入器
NBT的Python项目详细描述
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
描述:这是一个基于markus persson规范的命名二进制标记解析器。
- From The spec:
- “NBT (Named Binary Tag) is a tag based binary format designed to carry large
- amounts of binary data with smaller amounts of additional data. An NBT file consists of a single GZIPped Named Tag of type TAG_Compound.”
read the full spec at http://www.minecraft.net/docs/NBT.txt
- Usage:
- Reading files.
The easiest way to read an nbt file is to instantiate an NBTFile object e.g.
>>> import nbt >>> nbtfile = nbt.NBTFile("bigtest.nbt",'rb') >>> nbtfile.name u'Level' >>> nbtfile["nested compound test"].tag_info() TAG_Compound("nested compound test"): 2 Entries >>> for tag in nbtfile["nested compound test"]["ham"].tags: ... print(tag.tag_info()) ... TAG_String("name"): Hampus TAG_Float("value"): 0.75 >>> [tag.value for tag in nbtfile["listTest (long)"].value] [11, 12, 13, 14, 15]文件也可以从fileobj(包含压缩的 或缓冲区(包含NBT的未压缩流的类似文件的对象 标签)可通过以下方式完成:
>>> import nbt >>> nbtfile = NBTFile(fileobj=previously_opened_file) # or.... >>> nbtfile = NBTFile(buffer=net_socket.makefile())
- 正在写入文件。
写文件也很容易!如果你有一个nbtfile对象,只要调用它 write_file()方法。如果nbtfile是用文件名实例化的,那么 写入文件不需要额外的参数。它只是起作用。如果您创建了一个新的 文件对象从头开始(或者即使您只是想将其保存到其他地方) 调用write_file('pathtonewfile.nbt')
>>> import nbt >>> nbtfile = nbt.NBTFile("bigtest.nbt",'rb') >>> nbtfile["listTest (compound)"].tags[0]["name"].value = "Different name" >>> nbtfile.write_file("newnbtfile.nbt")也可以使用相同的关键字参数写入缓冲区或fileobj。
>>> nbtfile.write_file(fileobj = my_file) #compressed >>> nbtfile.write_file(buffer = sock.makefile()) #uncompressed
- 创建文件
创建文件比较麻烦,但最终应该不会给您带来任何问题,只要 你已经阅读了NBT规范(提示..很短)。还要注意 nbtfile对象实际上是一个带有一些包装特性的tag_复合物,因此 您可以使用所有标准标记功能
>>> from nbt import * >>> nbtfile = NBTFile()首先,不要忘记命名顶级标记
>>> nbtfile.name = "My Top Level Tag" >>> nbtfile.tags.append(TAG_Float(name="My Float Name", value=3.152987593947)) >>> mylist = TAG_List(name="TestList", type=TAG_Long) #type needs to be pre-declared! >>> mylist.tags.append(TAG_Long(100)) >>> mylist.tags.extend([TAG_Long(120),TAG_Long(320),TAG_Long(19)]) >>> nbtfile.tags.append(mylist) >>> print(nbtfile.pretty_tree()) TAG_Compound("My Top Level Tag"): 2 Entries { TAG_Float("My Float Name"): 3.15298759395 TAG_List("TestList"): 4 entries of type TAG_Long { TAG_Long: 100 TAG_Long: 120 TAG_Long: 320 TAG_Long: 19 } } >>> nbtfile["TestList"].tags.sort(key = lambda tag: tag.value) >>> print(nbtfile.pretty_tree()) TAG_Compound("My Top Level Tag"): 2 Entries { TAG_Float("My FloatName"): 3.15298759395 TAG_List("TestList"): 4 entries of type TAG_Long { TAG_Long: 19 TAG_Long: 100 TAG_Long: 120 TAG_Long: 320 } } >>> nbtfile.write_file("mynbt.dat")
平台:未知 分类器:开发状态::5-生产/稳定 分类器:目标受众::开发人员 分类器:许可证::OSI批准::MIT许可证 分类器:操作系统::与操作系统无关 分类器:编程语言::python 分类器:编程语言::python::2.7 分类器:编程语言::python::3.3 分类器:编程语言::python::3.4 分类器:编程语言::python::3.5 分类器:编程语言::python::3.6 分类器:主题::游戏/娱乐 分类器:topic::软件开发::库::python模块