gpt4 book ai didi

Python - 读取 .b4u 文件 - 找到错误序列项 0 : expected str instance, 字节

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:47 26 4
gpt4 key购买 nike

我正在尝试使用 http://grantcox.com.au/2012/01/decoding-b4u-binary-file-format/将 .b4u 文件导出为 HTML 格式的 Python 代码,但由于某种原因在程序点之后:

# find the initial caret position - this changes between files for some reason - search for the "Cards" string
for i in range(3):
addr = 104 + i*4
if ''.join(self.parser.read('sssss', addr)) == 'Cards':
caret = addr + 32
break
if caret is None:
return

我收到以下错误:

if ''.join(self.parser.read('sssss', addr)) == 'Cards':
TypeError: sequence item 0: expected str instance, bytes found

我使用的 Python 版本是:Python 3.3.1(v3.3.1:d9893d13c628,2013 年 4 月 6 日,20:25:12)。

知道如何解决这个问题吗?

最佳答案

我已经让它在 Python 2.7.4 下工作 我的 Python 3.3.2 也给我同样的错误。如果我发现如何将这段代码移植到 Python 3.x.x,我会尽快与您联系。这一定与 Python 3 中字符串的默认 unicode 有关。这是我想出的解决方案:

def read(self, fmt, offset):
if self.filedata is None:
return None
read = struct.unpack_from('<' + fmt, self.filedata, offset)
xread = []
for each in range(0,len(read)):
try:
xread.append(read[each].decode())
except:
xread.append(read[each])
read = xread
if len(read) == 1:
return read[0]
return read

def string(self, offset):
if self.filedata is None:
return None
s = u''
if offset > 0:
length = self.read('H', offset)
for i in range(length):
raw = self.read('H', offset + i*2 +2)
char = raw ^ 0x7E
s = s + chr(char)
return s

def plain_fixed_string(self, offset):
if self.filedata is None:
return None
plain_bytes = struct.unpack_from('<ssssssssssssssssssssssss', self.filedata, offset)
xplain_bytes = []
for each in range(0,len(plain_bytes)):
try:
xplain_bytes.append(plain_bytes[each].decode())
except:
xplain_bytes.append(plain_bytes[each])
plain_bytes = xplain_bytes
plain_string = ''.join(plain_bytes).strip('\0x0')
return plain_string

您可以直接使用这些方法,而不是原作者提供的方法。请注意,如果您在任何地方看到它,还应该将 unicode() 更改为 str() 并将 unichr() 更改为 chr()。还要记住,print 是一个函数,不能在没有括号 () 的情况下使用。

关于Python - 读取 .b4u 文件 - 找到错误序列项 0 : expected str instance, 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16125451/

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