Python 混合类型的编码数据?

Python 混合类型的编码数据?,python,Python,我有一个数组,如下所示: row = [u'Arun DC', 4.0, 34.0, 76.0, 223.52941176470588, u'yes', 0.0, '', '', '', '', u'yes', '', u'yes', u'yes', u'yes', u'no', u'\xa37.50 - \xa310 not clear what this is for', u'\xa37.50 - \xa310 not clear what this is for', '', u'\xa37

我有一个数组,如下所示:

row = [u'Arun DC', 4.0, 34.0, 76.0, 223.52941176470588, u'yes', 0.0, '', '', '', '', u'yes', '', u'yes', u'yes', u'yes', u'no', u'\xa37.50 - \xa310 not clear what this is for', u'\xa37.50 - \xa310 not clear what this is for', '', u'\xa37.50 - \xa310 not clear what this is for', u'\xa37.50 - \xa310 not clear what this is for', '', '', '', '', u'no water on sites', '', u'1st years rent free', '']
我想将其写入CSV文件,但出现以下错误:

writer.writerow(row)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128)
因此,我尝试先对其进行编码,但这给了我一个错误,因为我无法对浮动项进行编码:

writer.writerow([i.encode('latin-1') for i in row])
AttributeError: 'int' object has no attribute 'encode'
有没有什么好办法来解决这个问题

[i.encode('latin-1') if isinstance(i, unicode) else i for i in row]
返回字符串列表。您可以使用它将数据写入CSV

[x.encode('latin-1') if isinstance(x, unicode) else x
 for x in row]

返回字符串列表。你可以用它写出CSV。

是的,但这会完全过滤掉数字。好的观点,更新为使用三元if而不是列表理解的if。现在我们有两个答案,每个答案都有一个变量名。是的,但这会完全过滤掉数字。好的观点,更新为使用三元if,而不是列表中的if。现在我们有两个答案,它们在一个变量名上彼此不同。
[x.encode('latin-1') if isinstance(x, unicode) else x
 for x in row]