2017-04-12 105 views
0

我具有低於給定的字符串的列表:UnicodeEncodeError:在位置32 'ASCII' 編解碼器無法編碼的字符的u ' u2019':在範圍序數不(128)

[u'Any subscription charges to avail this facility', 
u'credited into the beneficiary\u2019s account', 
u'funds have been credited in the beneficiary\u2019s account', 
u'Can I reuse VPA'] 

有一些的unicode字符串中的字符(\ u2019)表示(')標點符號。請讓我知道如何刪除這個創建錯誤。我用下面的代碼刪除,但不起作用:

for x in mylist: 
    x.encode('ascii','ignore') 
    new_list.append(x) 

但它返回與unicode字符相同的列表。請幫助

+0

你能a)用簡單的'''替換'\ u2019'嗎?或b)使用'utf-8'而不是'ascii'編碼? –

+0

@MadPhysicist,你能告訴如何做到這一點?正如代碼 –

+1

'x.encode()'返回編碼結果,它不修改X:你想:'new_list.append(x.encode( 'ASCII', '忽略'))' – TemporalWolf

回答

0

您不會將編碼值追加到new_list。

for x in mylist: 
    new_list.append(x.encode('ascii','ignore')) 


['Any subscription charges to avail this facility', 
'credited into the beneficiarys account', 
'funds have been credited in the beneficiarys account', 
'Can I reuse VPA'] 
+0

僅有代碼的答案很少有幫助,尤其是考慮到它對未來讀者(可能有類似問題)有用的重點。如果您想回答問題,請查看[答案]並提供更多信息。 – TemporalWolf

相關問題