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沒有解碼方法。在Python中,如何將整數和字符串列表轉換爲Unicode?
我可以使用for循環與嘗試?或者我可以在列表壓縮中做一些明確的類型檢查,但是用類似Python的動態語言進行類型檢查是正確的方法嗎?
UPDATE:
我寧願整型的保持廉政的。雖然這不是一個嚴格的要求。我的理想輸出是[u'Some strings.', 1, 2, 3, u'More strings!', u'Fancÿ string!']
。
什麼是你想要的輸出? ''有些琴絃',1,2,3,u'More琴絃!']''''有些琴絃',u'1',u'2',u'3',u'更多琴絃!']','[''有些琴絃','更多琴絃!']'? – 2012-03-05 17:35:53