用于读取和编辑nbt数据的python包

nbtlib的Python项目详细描述


nbtlib

Build StatusPyPIPyPI - Python Version

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

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

推荐PyPI第三方库


热门话题
AmazonS3查找从S3forJava下载的压缩文件的MIME类型   java如何使用Selenium在<span>中查找具有特定文本的元素   python如何使用OpenIEDemo生成自定义三元组。由stanfordnlp提供的java   java遇到“方法不适用”编译错误   java如何使用Mockito在Looper中运行的验证代码。getMainLooper?   类Java目录错误   java在已知其他泛型信息时使用原始类型   java connect()和disconnect()在哪里实现?   java使用PDF Box库拆分PDF,生成的PDF几乎与源PDF文件大小相同   java PowerMockito返回错误的对象   java如何找到TIBCO集合消息的字节编码?   java Basic音乐播放器下一步和上一步按钮   添加模块描述符时,java没有名为“entityManagerFactory”的bean可用   java为什么我的代码不是线程安全的?   eclipse java。引用项目中的类的lang.NoClassDefFoundError