解码utf8编码字符串变量

2024-09-30 18:30:27 发布

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

在python中解码utf-8编码的字符串变量时遇到了一个问题。在

text = b'JAK SI\xc4\x98 \xe2\x80\x9eNAZYWA\xe2\x80\x9d?'

text.decode()
-> OK

text = str(text)
text.decode()

Error:'str' object has no attribute 'decode'

我只能访问字符串变量。如何从字符串变量中解码utf-8编码的文本?谢谢!在


Tags: 字符串text编码解码utfxe2decodestr
1条回答
网友
1楼 · 发布于 2024-09-30 18:30:27
>>> text = b'JAK SI\xc4\x98 \xe2\x80\x9eNAZYWA\xe2\x80\x9d?'
>>> text.decode()
'JAK SIĘ „NAZYWA”?'
>>> s = str(text)
"b'JAK SI\\xc4\\x98 \\xe2\\x80\\x9eNAZYWA\\xe2\\x80\\x9d?'"
>>> eval(s).decode()
'JAK SIĘ „NAZYWA”?'

这是你想要的吗?在

相关问题 更多 >