首页 > 解决方案 > 将存储为变量的 unicode 转换为纯文本(例如:“\u0044”到“D”)

问题描述

我正在从 .htm 文件中解析/提取 unicode,并将它们存储为变量。问题是,我希望它打印为纯文本/字母。这是我的代码:

with open("Peribahasa.htm", "r") as Peribahasa_File:
for line in itertools.islice(Peribahasa_File, 1000, 1150):
    for item in line.split("\n"):
        if "I[0][1][0][0]" in item:
            answer_1 = (((item.strip()).replace("I[0][1][0][0] = '", "")).replace("';", ""))
            print(answer_1.encode('utf-8'))

但是,最后的打印行将其打印为 -> b'\u0044' 作为参考, answer_1 变量是 -> \u0044

我只需要将 \u0044 转换为字母 D

任何帮助都将不胜感激!我已经在这几个小时了,谢谢!

我使用 Python 3

标签: pythonstringunicodeutf-8type-conversion

解决方案


尝试以下操作:

answer_1.encode().decode('unicode_escape')

推荐阅读