从二进制文件中读取UTF-8字符串

Sur*_*rma 3 python utf-8

我有一些文件包含一堆不同种类的二进制数据,我正在编写一个模块来处理这些文件.

其中,它包含以下格式的UTF-8编码字符串:2字节大端字符串stringLength(我使用struct.unpack()解析)然后是字符串.因为它是UTF-8,所以字符串的字节长度可能大于stringLength,如果字符串包含多字节字符,则读取(stringLength)会变短(更不用说弄乱文件中的所有其他数据) .

如何从文件中读取n个 UTF-8字符(与n个字节不同),知道UTF-8的多字节属性?我一直在谷歌搜索半小时,我发现的所有结果要么不相关,要么做出我无法做出的假设.

Mar*_*ers 5

给定一个文件对象和一些字符,您可以使用:

# build a table mapping lead byte to expected follow-byte count
# bytes 00-BF have 0 follow bytes, F5-FF is not legal UTF8
# C0-DF: 1, E0-EF: 2 and F0-F4: 3 follow bytes.
# leave F5-FF set to 0 to minimize reading broken data.
_lead_byte_to_count = []
for i in range(256):
    _lead_byte_to_count.append(
        1 + (i >= 0xe0) + (i >= 0xf0) if 0xbf < i < 0xf5 else 0)

def readUTF8(f, count):
    """Read `count` UTF-8 bytes from file `f`, return as unicode"""
    # Assumes UTF-8 data is valid; leaves it up to the `.decode()` call to validate
    res = []
    while count:
        count -= 1
        lead = f.read(1)
        res.append(lead)
        readcount = _lead_byte_to_count[ord(lead)]
        if readcount:
            res.append(f.read(readcount))
    return (''.join(res)).decode('utf8')
Run Code Online (Sandbox Code Playgroud)

测试结果:

>>> test = StringIO(u'This is a test containing Unicode data: \ua000'.encode('utf8'))
>>> readUTF8(test, 41)
u'This is a test containing Unicode data: \ua000'
Run Code Online (Sandbox Code Playgroud)