2016-05-12 114 views
0

我知道這是一個常見的初學者問題,這裏有很多像這樣的問題在堆棧交換,我一直在尋找他們,但我仍然無法確定這個出。我從刮,看起來像這樣(約1000個項目列表中的)一些數據:ascii編解碼器不能編碼字符,python 2.6

inputList = [[u'someplace', u'3901 West Millen Drive', u'Hobbs', u'NH', 
u'88240', u'37.751117', u'-103.187709999'], [u'\u0100lon someplace', u'3120 
S Las Vegas Blvd', u'Las Duman', u'AL', u'89109', u'36.129066', u'-145.168791']] 

我試圖將其寫入到一個CSV文件是這樣的:

for i in inputList: 
    for ii in i: 
     ii.replace(" u'\u2019'", "") #just trying to get rid of offending character 
     ii.encode("utf-8") 

def csvWrite(inList, outFile): 
    import csv 
    destination = open(outFile, 'w') 
    writer = csv.writer(destination, delimiter = ',') 
    data = inList 
    writer.writerows(data) 
    destination.close() 
csvWrite(inputList, output) 

,但我不斷收到此錯誤上,writer.writerows(數據):

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in 
position 5: ordinal not in range(128) 

我已經嘗試了一堆不同的東西在列表中的數據進行編碼,但仍然一直得到錯誤。我打算忽略無法編碼爲ascii的字符。任何人都可以指向我正確的方向,我使用python2.6

+0

是的,我的意思是這樣做,但我仍然得到同樣的錯誤後修復該行 –

回答

1

這條線看起來很奇怪:ii.replace(" u'\u2019'", ""),你的意思是ii.replace(u"\u2019", u"")

+0

這不會工作要麼 – Veltro

-1

,如果你只是想刪除這些壞字符,你可以使用此代碼來代替:

for i in inputList: 
    for ii in i: 
     ii = "".join(list(filter((lambda x: ord(x) < 128), ii))) 
     print ii 

輸出:

someplace 
3901 West Millen Drive 
Hobbs 
NH 
88240 
37.751117 
-103.187709999 
lon someplace 
3120 S Las Vegas Blvd 
Las Duman 
AL 
89109 
36.129066 
-145.168791 

最終代碼看起來像這樣:

inputList = [[u'someplace', u'3901 West Millen Drive', u'Hobbs', u'NH', 
u'88240', u'37.751117', u'-103.187709999'], [u'\u0100lon someplace', u'3120 S Las Vegas Blvd', u'Las Duman', u'AL', u'89109', u'36.129066', u'-145.168791']] 

cleared_inputList = [] 

for i in inputList: 
    c_i = [] 
    for ii in i: 
     ii = "".join(list(filter((lambda x: ord(x) < 128), ii))) 
     c_i.append(ii) 
    cleared_inputList.append(c_i) 

def csvWrite(inList, outFile): 
    import csv 
    destination = open(outFile, 'w') 
    writer = csv.writer(destination, delimiter = ',') 
    data = inList 
    writer.writerows(data) 
    destination.close() 


csvWrite(cleared_inputList, output) 
+0

這仍然會引發字符u'\ u2019' –

+0

@ J.Hammond錯誤,這是因爲您沒有更新'inputList',我會編輯我的答案。 – Veltro

+0

該代碼現在工作正常@ J.Hammond – Veltro

相關問題