[英]str to bytes in Python3.3
如何從'\\xe3\\x81\\x82'
b'\\xe3\\x81\\x82'
獲取b'\\xe3\\x81\\x82'
'\\xe3\\x81\\x82'
?
最后,我想要u'\あ'
,這意味着日文字母'あ',
b'\\xe3\\x81\\x82'.decode('utf-8')
讓u'\あ'
但是
'\\xe3\\x81\\x82'.decode('utf-8')
導致以下錯誤
AttributeError: 'str' object has no attribute 'decode'
因為b'\\xe3\\x81\\x82'
是字節而'\\xe3\\x81\\x82'
是str。
我的數據庫包含'\\xe3\\x81\\x82'
。
如果您將字節偽裝成Unicode代碼點,則編碼為Latin-1:
'\xe3\x81\x82'.encode('latin1').decode('utf-8')
Latin-1(ISO-8859-1)將Unicode代碼點一對一映射到字節:
>>> '\xe3\x81\x82'.encode('latin1').decode('utf-8')
'あ'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.