[英]str to bytes in Python3.3
How can I get b'\\xe3\\x81\\x82'
from '\\xe3\\x81\\x82'
? 如何从'\\xe3\\x81\\x82'
b'\\xe3\\x81\\x82'
获取b'\\xe3\\x81\\x82'
'\\xe3\\x81\\x82'
?
Finally, I want u'\あ'
, which means Japanese letter 'あ', 最后,我想要u'\あ'
,这意味着日文字母'あ',
b'\\xe3\\x81\\x82'.decode('utf-8')
makes u'\あ'
but b'\\xe3\\x81\\x82'.decode('utf-8')
让u'\あ'
但是
'\\xe3\\x81\\x82'.decode('utf-8')
causes the following error '\\xe3\\x81\\x82'.decode('utf-8')
导致以下错误
AttributeError: 'str' object has no attribute 'decode'
because b'\\xe3\\x81\\x82'
is bytes and '\\xe3\\x81\\x82'
is str. 因为b'\\xe3\\x81\\x82'
是字节而'\\xe3\\x81\\x82'
是str。
I have DB with data like '\\xe3\\x81\\x82'
. 我的数据库包含'\\xe3\\x81\\x82'
。
If you have bytes disguising as Unicode code points, encode to Latin-1: 如果您将字节伪装成Unicode代码点,则编码为Latin-1:
'\xe3\x81\x82'.encode('latin1').decode('utf-8')
Latin-1 (ISO-8859-1) maps Unicode codepoints one-on-one to bytes: Latin-1(ISO-8859-1)将Unicode代码点一对一映射到字节:
>>> '\xe3\x81\x82'.encode('latin1').decode('utf-8')
'あ'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.