相关疑难解决方法(0)

如何将 unicode 转换为 unicode 转义文本

我正在加载一个包含一堆 unicode 字符的文件(例如\xe9\x87\x8b)。我想在 Python 中将这些字符转换为其转义 unicode 形式 ( \u91cb)。我在 StackOverflow 上发现了几个类似的问题,包括Evaluate UTF-8 Literal escape strings in a string in Python3,它几乎完全符合我的要求,但我不知道如何保存数据。

例如: 输入文件:

\xe9\x87\x8b

Python脚本

file = open("input.txt", "r")
text = file.read()
file.close()
encoded = text.encode().decode('unicode-escape').encode('latin1').decode('utf-8')
file = open("output.txt", "w")
file.write(encoded) # fails with a unicode exception
file.close()
Run Code Online (Sandbox Code Playgroud)

输出文件(我想要的):

\u91cb

python unicode python-3.x

3
推荐指数
1
解决办法
6804
查看次数

在python中将“\x”转义字符串转换为可读字符串

有没有办法将\x转义字符串转换"\\xe8\\xaa\\x9e\\xe8\\xa8\\x80"为可读形式:"??"

>>> a = "\\xe8\\xaa\\x9e\\xe8\\xa8\\x80"
>>> print(a)
\xe8\xaa\x9e\xe8\xa8\x80
Run Code Online (Sandbox Code Playgroud)

我知道,有一个类似的问题在这里,但似乎该解决方案仅适用于拉丁字符。如何将这种形式的字符串转换为可读的 CJK 字符?

python unicode encoding utf-8

3
推荐指数
1
解决办法
621
查看次数

标签 统计

python ×2

unicode ×2

encoding ×1

python-3.x ×1

utf-8 ×1