从二进制fi读取UTF8字符串

2024-09-29 19:35:51 发布

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

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

其中,它包含以下格式的UTF-8编码字符串:2字节big-endianstringLength(我用它来解析解包结构())然后是字符串。因为它是UTF-8,所以字符串的字节长度可能会大于stringLength,如果字符串包含多字节字符,那么read(stringLength)就会很短(更不用说搞乱文件中的所有其他数据)。在

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


Tags: 模块文件数据字符串编码字节格式二进制
2条回答

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

# 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')

测试结果:

^{pr2}$

UTF-8中的一个字符可以是1字节、2字节、3字节3。在

如果必须逐字节读取文件,则必须遵循UTF-8编码规则。http://en.wikipedia.org/wiki/UTF-8

大多数时候,您只需将编码设置为utf-8,然后读取输入流。在

你不需要关心你读了多少字节。在

相关问题 更多 >

    热门问题