python - 如何在python中将unicode转换为utf8?

标签 python unicode utf-8 xbmc

我正在编写 python 脚本,从 sqlite3 数据库中提取 xbmc 媒体应用程序的数据。

我可以看到,在我的代码中,它将使用 unicode 对象提取数据,其中我将拥有字符串 (u'uL.

我想将它从 unicode 对象转换回普通字符串到 utf8。

这是代码:

programs = None
daysLimit = 14
start = datetime.datetime.now()
end = start + datetime.timedelta(days = daysLimit)
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel')
programs = cur.fetchall()

print(programs)
cur.close()

这是 xbmc 日志:

03:49:03 T:3628  NOTICE: [(u'101 ABC FAMILY ', u'The Middle -  The Ditch',
20140520170000L, 20140520173000L), (u'101 ABC FAMILY ', u'The Goonies', 
20140520173000L, 20140520200000L), (u'101 ABC FAMILY ', u'Pirates of the Caribbean: On Stranger Tides', 
20140520200000L, 20140520230000L), (u'101 ABC FAMILY ', u'The 700 Club', 
20140520230000L, 20140521000000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  Day Damn One', 
20140521000000L, 20140521003000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  Lucky Charm', 
20140521003000L, 20140521010000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  The Ethnic Tip', 
20140521010000L, 20140521013000L), (u'101 ABC FAMILY ', u'The Fresh Prince of Bel-Air -  The Young and the Restless', 
20140521013000L, 20140521020000L), (u'101 ABC FAMILY ', u'Summer Sexy With T25!', 
20140521020000L, 20140521023000L), (u'101 ABC FAMILY ', u'Paid Programming', 
20140521023000L, 20140521030000L)

我想忽略字符串 (u', uL 所以我想让它看起来像这样:

'101 ABC FAMILY ', 'The Middle -  The Ditch', 20140520170000, 20140520173000, 
'101 ABC FAMILY ', 'The Goonies', 20140520173000, 20140520200000, 
'101 ABC FAMILY ', 'Pirates of the Caribbean: On Stranger Tides', 20140520200000, 20140520230000, 
'101 ABC FAMILY ', 'The 700 Club', 20140520230000, 20140521000000, 
'101 ABC FAMILY ', 'The Fresh Prince of Bel-Air -  Day Damn One', 20140521000000, 20140521003000,
and so on...

你能告诉我如何使用 python 2.6 版本将 unicode 对象转换为 utf8 吗?

最佳答案

  • L 后缀表示长整数。它们实际上与(短)整数相同;确实没有必要转换这些。只有他们的 repr() 输出包含 L; 直接打印该值或将其写入文件且不包含L后缀。

  • 可以使用 unicode.encode() 方法将 Unicode 值编码为 UTF-8:

    encoded = unicodestr.encode('utf8')
    

您的问题与此处的列表表示有关;您记录了所有行,Python 容器通过对每个值调用 repr() 来表示其内容。这些表示非常适合调试,因为它们的类型很明显。

这取决于您接下来如何处理这些值。。通常最好在整个代码中使用 Unicode,并且仅在最后一刻(写入文件、打印或通过网络发送时)进行编码。有很多方法可以为您处理这个问题。例如,打印将自动编码到您的终端编解码器。添加到 XML 文件时,大多数 XML 库都会为您处理 Unicode。等等

关于python - 如何在python中将unicode转换为utf8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268642/

相关文章:

python - 有人有 ssl 使用 Python Cassandra 驱动程序和 eventlet 吗?

python - 如何在 tensorflow 数据集的每次迭代中添加随机性?

python - 如何模拟金库 hvac.Client() 方法

python - 在 DataFrame Pandas 中选择多列。切片 + 选择

c# - 尽管原始类型如何,我如何强制将每个字符串编码为 UTF8?

c - 从 C 文件中读取 unicode 字符

ruby - 发送到服务器的 Unicode 字符作为垃圾返回

python - CSV 文件的 Unicode 到 UTF8 - Python 通过 xlrd

python - 读取各种不同编码的文件

jsf - 向托管 bean 发送 UTF-8 字符串不起作用