用于读取和编辑nbt数据的python包
nbtlib的Python项目详细描述
nbtlib
A python library to read and edit nbt data. Requires python 3.6.
功能
- 创建、读取和编辑NBT文件
- 支持gzipped和uncompressed文件
- 支持big-endian和little-endian文件
- 分析并序列化原始NBT数据
- 定义自动强制执行预定义标记类型的标记架构
- 在二进制形式和文字表示法之间转换nbt
- 包括一个用于快速执行读/写/合并操作的cli
安装
可以使用pip
安装包。
$ pip install nbtlib
基本用法
下面的例子将为您提供一个非常基本的概述 可以。有关更高级的示例,请查看 “Usage” 文档文件夹中的笔记本。
读取文件
可以使用nbtlib.load
函数将nbt文件加载为nbtlib.File
对象。这个
root
属性包含根nbt标记。每个NBT标记继承自
它的python对应项。这意味着在python对应项上定义的所有内置操作都可以在nbt标记上使用。
importnbtlibnbt_file=nbtlib.load('bigtest.nbt')assertnbt_file.root['intTest']==2147483647
例如,nbtlib.File
的实例继承自常规的Compound
标记,这些标记本身继承自内置的python字典dict
。类似地,Int
标记的实例继承自内置类int
。
有关加载nbt文件和如何使用nbt标记的详细信息,请查看“Usage” 笔记本。
编辑文件
可以将nbtlib.File
的实例用作上下文管理器以保存修改
在with
语句末尾自动执行。
importnbtlibfromnbtlib.tagimportIntwithnbtlib.load('demo.nbt')asdemo:demo.root['counter']=Int(demo.root['counter']+1)
您还可以手动调用save
方法。
importnbtlibfromnbtlib.tagimportIntdemo=nbtlib.load('demo.nbt')demo.root['counter']=Int(demo.root['counter']+1)demo.save()
有关save
方法的更多详细信息,请查看“Usage”
笔记本。
使用模式
nbtlib
允许您定义强制特定标记类型的Compound
架构
对于任何给定的密钥。
fromnbtlibimportschemafromnbtlib.tagimportShort,StringMySchema=schema('MySchema',{'foo':String,'bar':Short})my_object=MySchema({'foo':'hello world','bar':21})assertisinstance(my_object,MySchema)assertisinstance(my_object['foo'],String)
有关架构的更多详细信息,请查看“Usage” 笔记本。
NBT文字
可以使用nbtlib.parse_nbt
函数解析nbt文本。
fromnbtlibimportparse_nbtfromnbtlib.tagimportString,List,Compound,IntArraymy_compound=parse_nbt('{foo: [hello, world], bar: [I; 1, 2, 3]}')assertmy_compound==Compound({'foo':List[String](['hello','world']),'bar':IntArray([1,2,3])})
nbt标记可以通过nbtlib.serialize_tag
函数序列化为它们的文本表示。
fromnbtlibimportserialize_tagfromnbtlib.tagimportString,List,Compound,IntArraymy_compound=Compound({'foo':List[String](['hello','world']),'bar':IntArray([1,2,3])})assertserialize_tag(my_compound)=='{foo: ["hello", "world"], bar: [I; 1, 2, 3]}'
有关nbt文本的更多详细信息,请查看“Usage” 笔记本。
命令行界面
该软件包附带一个小的cli,使其易于快速执行 NBT文件的基本操作。
$ nbt --help
usage: nbt [-h] (-r | -w <nbt> | -m <nbt>) [--plain] [--little] [--pretty]
[--compact]
<file>
Perform basic operations on nbt files.
positional arguments:
<file> the target file
optional arguments:
-h, --help show this help message and exit
-r read nbt data from a file
-w <nbt> write nbt to a file
-m <nbt> merge nbt into an nbt file
--plain don't use gzip compression
--little use little-endian format
--pretty output indented snbt
--compact output compact snbt
读取NBT数据
您可以使用-r
选项读取nbt文件。这将打印
二进制NBT数据的文字表示法。
$ nbt -r my_file.nbt {foo: [1, 2, 3], bar: "Hello, world!"}
如果要将输出保存到 文件。
$ nbt -r my_file.nbt > my_file.txt
使用--compact
参数将从输出中删除所有多余的空白。
$ nbt -r my_file.nbt --compact {foo:[1,2,3],bar:"Hello, world!"}
如果希望命令输出缩进的snbt,可以使用--pretty
参数。
$ nbt -r my_file.nbt --pretty { foo: [1, 2, 3], bar: "Hello, world!"}
写入NBT数据
您可以使用-w
选项将nbt数据写入文件。这个遗嘱
将文字NBT表示法转换为其二进制形式并将其保存在
指定的文件。
$ nbt -w '{foo:[1,2,3],bar:{hello:[B;1b,1b,0b,1b]}}' my_file.nbt
如果文件不存在,文件将被创建。
合并NBT数据
<>最后,您可以将一些NBT数据合并到已经存在的文件中。 使用-m
选项。这将递归地用
从literal参数解析的值。$ nbt -m '{bar:{"new key":56f}}' my_file.nbt
您可以使用-r
选项检查结果。
$ nbt -r my_file.nbt {foo: [1, 2, 3], bar: {hello: [B; 1B, 1B, 0B, 1B], "new key": 56.0f}}
这里,输入文本中不存在的复合值是
保持原样。使用-w
选项而不是-m
将
覆盖整个文件。
压缩和字节顺序
默认情况下,cli将假定您使用的是gzipped nbt
文件夹。如果要读取、写入或合并未压缩的NBT文件,则
可以使用--plain
选项。类似地,默认字节顺序是
big endian,因此需要使用--little
选项来执行
对小endian文件的操作。
正在读取
$ nbt -r my_file.nbt --plain --little {name: "Reading an uncompressed little-endian file"}
写入
$ nbt -w '{name:"Writing in an uncompressed little-endian file"}' my_file.nbt --plain --little
合并
$ nbt -m '{name:"Merging in an uncompressed little-endian file"}' my_file.nbt --plain --little
贡献
欢迎捐款。此项目使用^{
$ curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
现在您应该能够安装所需的依赖项。
$ poetry install
您可以使用poetry run pytest
运行测试。
$ poetry run pytest
许可证-MIT