[英]Decoding utf-8 encoded string variables
我在python中解碼utf-8編碼字符串變量時遇到問題。
text = b'JAK SI\xc4\x98 \xe2\x80\x9eNAZYWA\xe2\x80\x9d?'
text.decode()
-> OK
text = str(text)
text.decode()
錯誤:“ str”對象沒有屬性“ decode”
我只能訪問字符串變量。 如何從字符串變量中解碼utf-8編碼的文本? 謝謝!
>>> 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”?'
這就是你想要的嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.