【发布时间】:2012-03-23 04:34:38
【问题描述】:
x = ['Some strings.', 1, 2, 3, 'More strings!', 'Fanc\xc3\xbf string!']
y = [i.decode('UTF-8') for i in x]
将 x 中的字符串转换为 Unicode 的最佳方法是什么?执行列表压缩会导致属性错误 (AttributeError: 'int' object has no attribute 'decode'),因为 int 没有解码方法。
我可以尝试使用 for 循环吗?或者我可以在列表压缩中进行一些显式类型检查,但是在 Python 这样的动态语言中进行类型检查是正确的方法吗?
更新:
我希望 int 保持 int 的状态。虽然这不是一个严格的要求。我的理想输出是[u'Some strings.', 1, 2, 3, u'More strings!', u'Fancÿ string!']。
【问题讨论】:
-
你想要的输出是什么?
[u'Some strings', 1, 2, 3, u'More strings!'],[u'Some strings', u'1', u'2', u'3', u'More strings!'],[u'Some strings', u'More strings!']?