我有一個數組如下:混合型編碼數據?
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'
任何想法來解決這個問題?
是的,但是這完全過濾了數字。 – Nate
好點,更新使用三元,而不是列表理解的一個。 –
現在我們有2個答案,這兩個答案的區別在於一個變量名。 – utapyngo