简体   繁体   English

如何将这个unicode字符串从数据库转换为utf-8

[英]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 通过遍历对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 : 我需要将其转换为utf-8,特别是u'HS 09 - Coffee, Tea, Mat\\xe9 and Spices'的第二个字段u'HS 09 - Coffee, Tea, Mat\\xe9 and Spices' ,结果文本应为:

'HS 09 - Coffee, Tea, Maté and Spices' “ HS 09-咖啡,茶,马黛和香料”

How can this be done? 如何才能做到这一点?

Use .encode('utf-8') . 使用.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. 有关术语的注释-数据库调用的结果为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. 您想要将unicode对象转换(编码)为utf-8的问题文本是正确的,但是标题有点不对。 I edited it to reflect this - a utf-8 encoded bytestring is not a Unicode string. 我对其进行了编辑以反映这一点-utf-8编码的字节字符串不是Unicode字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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