简体   繁体   中英

How do I convert this unicode string from a database to utf-8

I'm getting the following from iterating through the items in a database call to sqlite3

(u'9', u'HS 09 - Coffee, Tea, Mat\xe9 and Spices', u'Bangladesh', 2000, 6127)

I need to convert it to utf-8, specifically, the second field u'HS 09 - Coffee, Tea, Mat\\xe9 and Spices' The resulting text should be :

'HS 09 - Coffee, Tea, Maté and Spices'

How can this be done?

Use .encode('utf-8') . EG:

>>> u'HS 09 - Coffee, Tea, Mat\xe9 and Spices'.encode('utf-8')
'HS 09 - Coffee, Tea, Mat\xc3\xa9 and Spices'

A note on terminology - the results of your database call are unicode. Your question text is correct that you want to convert (encode) the unicode object into utf-8, but your header was a bit off. I edited it to reflect this - a utf-8 encoded bytestring is not a Unicode string.

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