【问题标题】:Edit Minecraft .dat File in Python在 Python 中编辑 Minecraft .dat 文件
【发布时间】:2022-10-05 05:26:48
【问题描述】:

我正在寻找在 python 中编辑 Minecraft Windows 10 level.dat 文件。我尝试使用包nbtpyanvil,但得到错误OSError: Not a gzipped file。如果我打印 open("level.dat", "rb").read() 我会得到很多无意义的数据。似乎它需要以某种方式解码,但我不知道它需要什么解码。如何打开(最好是编辑)这些文件之一?

【问题讨论】:

    标签: python python-3.x minecraft


    【解决方案1】:

    您必须提供相对于当前工作目录的路径

    路径/到/file.dat

    或者您可以使用文件的绝对路径

    C:user/dir/path/to/file.dat

    读取数据,替换值然后写入

    # Read in the file
    with open('file.dat', 'r') as file :
      filedata = file.read()
    
    # Replace the target string
    filedata = filedata.replace('yuor replacement or edit')
    
    # Write the file out again
    with open('file.dat', 'w') as file:
      file.write(filedata)
    

    希望这可以帮助

    【讨论】:

    • 你好!感谢您的洞察力,但我仍然得到无意义的数据。我不确定 minecraft 和其他编辑器如何解码 minecraft .dat 文件,但我仍然缺少一些东西。
    【解决方案2】:

    要读取数据,只需:

    from nbt import nbt
    
    nbtfile = nbt.NBTFile("level.dat", 'rb')
    print(nbtfile) # Here you should get a TAG_Compound('Data')
    print(nbtfile["Data"].tag_info()) # Data came from the line above
    for tag in nbtfile["Data"].tags: # This loop will show us each entry 
        print(tag.tag_info())
    

    至于编辑:

    # Writing data (changing the difficulty value
    nbtfile["Data"]["Difficulty"].value = 2
    print(nbtfile["Data"]["Difficulty"].tag_info())
    nbtfile.write_file("level.dat")
    

    如果您需要更多帮助,请告诉我...

    【讨论】:

    • 我仍然得到无意义的数据。我不确定 minecraft 和世界编辑器如何解码 minecraft .dat 文件,但我们仍然缺少一些东西。
    • 啊,我明白了,我会编辑我的回复
    • 它似乎仍然不起作用:gzip.BadGzipFile: Not a gzipped file (b'\x08\x00')
    • 请分享您的代码
    • 我的代码正是你发布的!
    猜你喜欢
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2021-12-09
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2021-10-09
    相关资源
    最近更新 更多