Python3.3中的str到字节

Python3.3中的str到字节,python,string,unicode,byte,encode,Python,String,Unicode,Byte,Encode,如何从'\xe3\x81\x82'获取b'\xe3\x81\x82' 最后,我想要u'\u3042',意思是日语字母'あ', b'\xe3\x81\x82'.解码('utf-8')makeu'\u3042' “\xe3\x81\x82”。解码('utf-8')导致以下错误 AttributeError: 'str' object has no attribute 'decode' 因为b'\xe3\x81\x82'是字节,'\xe3\x81\x82'是str 我有一个数据库,它的数据类似于'\

如何从
'\xe3\x81\x82'
获取
b'\xe3\x81\x82'

最后,我想要
u'\u3042'
,意思是日语字母'あ',

b'\xe3\x81\x82'.解码('utf-8')
make
u'\u3042'

“\xe3\x81\x82”。解码('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')
'あ'