将数字utf8序列转换为字符串

2024-06-28 11:04:36 发布

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

我需要转换这类字符串(unicode字符以特殊方式存储):

Ce correspondant a cherch=C3=A9 =C3=A0 vous joindre

。。。到有效的utf-8字符串,如下所示:

^{pr2}$

我编写了从这个简单语法中提取数字utf-8序列的代码 (=XX=XX,每个X都是一个十六进制数字),但是当我试图转换它时,我被卡住了 序列到可打印字符:它是一个utf-8序列,而不是Unicode代码点,因此chr() 内置在这里没有用处(至少,不是单独使用)。在

简而言之:

我需要转换这个示例值:

utf8_sequence = 0xC3A9

到此字符串:

return_value = 'é'

这个字母的Unicode码位是U+00E9,但我不知道如何从 此给定Unicode代码点的utf-8序列,可与chr()一起使用。在

我的代码

下面是我的代码,其中有一条注释显示了我陷入困境的地方:

#!/usr/bin/python3
# coding: utf-8

import re

test_string = 'Ce correspondant a cherch=C3=A9 =C3=A0 vous joindre'


# SHOULD convert a string like '=C3=A9' to the equivalent Unicode 
# char, in this example 'é'.
def vmg_to_unicode(in_string):

    whole_sequence = 0 # Stores the numerical utf-8 sequence
    in_length = len(in_string)
    num_bytes = int(in_length / 3) # Number of bytes
    bit_weight = num_bytes << 3 # Weight of char in bits (little-endian)

    for i in range(0, in_length, 3): # For each char:
        bit_weight -= 8
        # Extract the hex number inside '=XX':
        hex_number = in_string[i+1:][:2]
        # Build the utf-8 sequence:
        whole_sequence += int(hex_number, 16) << bit_weight

    # At this point, whole_sequence contains for example 0xC3A9

    # The following doesn't work, chr() expect a Unicode code point:
    # return chr(whole_sequence)

    # HOW CAN I RETURN A STRING LIKE 'é' THERE?

    # Only for debug:
    return '[0x{:X}]'.format(whole_sequence)


# In a whole string, convert all occurences of patterns like '=C3=A9'
# to their equivalent Unicode chars.
def vmg_transform(in_string):

    # Get all occurences:
    results = ( m for m in re.finditer('(=[0-9A-Fa-f]{2})+', in_string) )

    index, out = (0, '')

    for result in results:
        # Concat the unchanged text:
        out += in_string[index:result.start()]
        # Concat the replacement of the matched pattern:
        out += vmg_to_unicode(result.group(0))
        index = result.end()

    # Concat the end of the unchanged string:
    out += in_string[index:]

    return out


if __name__ == '__main__':
    print('In  : "{}"'.format(test_string))
    print('Out : "{}"'.format(vmg_transform(test_string)))

电流输出

In  : "Ce correspondant a cherch=C3=A9 =C3=A0 vous joindre"
Out : "Ce correspondant a cherch[0xC3A9] [0xC3A0] vous joindre"

期望输出

In  : "Ce correspondant a cherch=C3=A9 =C3=A0 vous joindre"
Out : "Ce correspondant a cherché à vous joindre"

Tags: the代码instringunicodeutfa9ce
1条回答
网友
1楼 · 发布于 2024-06-28 11:04:36
  • 首先创建一个bytearray
  • 填充它
  • 然后转换为bytes,并根据UTF-8编码进行解码

以下是您需要修改的部分代码:

    s = bytearray()

    for i in range(0, in_length, 3): # For each char:
        bit_weight -= 8
        # Extract the hex number inside '=XX':
        hex_number = in_string[i+1:][:2]
        # Build the utf-8 sequence:
        s.append(int(hex_number,16))

    # At this point, whole_sequence contains for example 0xC3A9

    # The following doesn't work, chr() expect a Unicode code point:
    # return chr(whole_sequence)

    # HOW CAN I RETURN A STRING LIKE 'é' THERE?

    # Only for debug:
    return bytes(s).decode("utf-8")

结果:

^{pr2}$

相关问题 更多 >