Python Read.b4u文件错误序列项0:应为str实例,找到字节

2024-10-08 18:24:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

# 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

我得到以下错误:

^{pr2}$

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

你知道怎么解决这个问题吗?在


Tags: thecomformathttpforiffileau
1条回答
网友
1楼 · 发布于 2024-10-08 18:24:53

我已经在python2.7.4下运行了python3.3.2也给了我同样的错误。如果我知道如何将这段代码移植到python3.x.x一定会与unicode作为python3中字符串的默认值有关。 我想出了一个解决方案:

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(),也应该将unicode()更改为str(),并将unichr()更改为chr()。还要记住print是一个函数,不能没有括号()就使用它。在

相关问题 更多 >

    热门问题