繁体   English   中英

str3到Python3.3中的字节

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM