Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Okay, I have read through many similar questions, and I believe I am following the advice correctly, but somehow my code is still not working.

I have parsed an xml file. I have read on here that the output is now unicode. I am using the csv writer to write output to a file.

So, in my code I have tried to encode in utf-8 before using writerow. Why do I still get the error on writerow? My warning, "unicode!!!" does not get thrown until this error happens (I am running this on multiple files, and it works for most). Actually, though, I don't understand why the writerow is trying to use ascii, shouldn't it be expecting utf-8? I have replaced utf-8 with ascii in the encode function just for kicks. Same results. Please help!!!

        try:

           mystring=elem.find('./'+r2+'Description').text


           if isinstance(mystring, unicode):
               print("unicode!!!")
               mystring.encode('utf-8','ignore')
               datalist.append(mystring)
           else:    
               datalist.append(mystring)
        except AttributeError:
           datalist.append('No text')  

        c.writerow(datalist)
like image 747
user1106322 Avatar asked Dec 19 '11 16:12

user1106322


1 Answers

When you call mystring.encode(..., it's not changing the string in-place; it returns a new string.

like image 90
DNS Avatar answered Sep 24 '22 14:09

DNS