gpt4 book ai didi

python - 如何从文件中读取字节

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:52 24 4
gpt4 key购买 nike

<分区>

我正在尝试从 .lrf 文件中读取一些元数据的长度。 (与程序 LoLReplay 一起使用)

这些文件并没有真正的文档,但我已经知道如何在 C++ 中执行此操作。出于多种原因,我试图用 Python 重新编写项目,但我遇到了一个错误。

首先解释一下,.lrf 文件的元数据紧接在文件的开头,格式如下:

  • 前 4 个字节用于我不知道的内容。

  • 接下来的 4 个字节以十六进制形式存储元数据的长度,直到元数据结束,之后是回放的实际内容。

  • 前8个字节后的字节是json格式的元数据

我遇到的问题实际上是读取元数据长度。这是我当前的功能:

def getMetaLength(self):
try:
file = open(self.file,"r")
except IOError:
print ("Failed to open file.")
file.close()
#We need to skip the first 4 bytes.
file.read(4)
mdlength = file.read(4)
print(hex(mdlength))
file.close()

当我调用这个函数时,shell 返回一个回溯说明:

    Traceback (most recent call last):
File "C:\Users\Donald\python\lolcogs\lolcogs_main.py", line 6, in <module>
lolcogs.getMetaLength()
File "C:\Users\Donald\python\lolcogs\LoLCogs.py", line 20, in getMetaLength
file.read(4)
File "C:\Python32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 3648: character maps to <undefined>

我最好的猜测是 read() 试图读取以某种 unicode 格式编码的字符,但这些绝对只是我试图读取的字节。有没有办法将这些读取为字节?另外,在尝试读取文件时是否有更好的方法来跳过字节?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com