命名二进制标记读取器/写入器

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:
  1. 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())
  1. 正在写入文件。

写文件也很容易!如果你有一个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
  1. 创建文件

创建文件比较麻烦,但最终应该不会给您带来任何问题,只要 你已经阅读了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模块

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
无法使用windows x86上的Java验证在linux x64上签名的数字签名   java Firebase通过键从子级检索数据   Java缓存网络文件   java独立jdbcpool实现内存泄漏   java为什么MVN在成功构建时返回1?   java maven正在抛出BoundsException数组   Java:是否可以有“嵌套”映射方法(Java.util.stream)?   java无法使用GSON解析JSON文件   java Spring Groovy集成   java使用SAX解析器,需要所有xml元素(在特定元素下)作为字符串   java列出内容提供商Android 3.1中的内容   java如何使用带有spring api的JPA查询进行软删除?   java隐藏我的socket构造函数以隐藏IP和端口   Java将(JEP359)记录为Spring控制器请求和响应DTO   java在数组中循环时忽略空值   RESTAPI端点的java发现模式   java HTTPs在jmeter中发布带有头的请求测试,并将测试结果保存在mongodb中,并在grafana中显示