简体   繁体   中英

str to bytes in Python3.3

How can I get b'\\xe3\\x81\\x82' from '\\xe3\\x81\\x82' ?

Finally, I want u'\あ' , which means Japanese letter 'あ',

b'\\xe3\\x81\\x82'.decode('utf-8') makes u'\あ' but

'\\xe3\\x81\\x82'.decode('utf-8') causes the following error

AttributeError: 'str' object has no attribute 'decode'

because b'\\xe3\\x81\\x82' is bytes and '\\xe3\\x81\\x82' is str.

I have DB with data like '\\xe3\\x81\\x82' .

If you have bytes disguising as Unicode code points, encode to Latin-1:

'\xe3\x81\x82'.encode('latin1').decode('utf-8')

Latin-1 (ISO-8859-1) maps Unicode codepoints one-on-one to bytes:

>>> '\xe3\x81\x82'.encode('latin1').decode('utf-8')
'あ'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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