2017-08-01 33 views
1

我在解碼python中的utf-8編碼字符串變量時遇到問題。解碼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編碼的文本?謝謝!

+0

你在用什麼語言工作?請將它添加到標籤 –

+0

在Python 3中,'''bytes'對象具有解碼方法,'str'對象不具有。 – cco

回答

0
>>> 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」?' 

這是你想要的嗎?