user1106322
user1106322

Reputation: 71

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)

Upvotes: 7

Views: 9312

Answers (1)

DNS
DNS

Reputation: 38189

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

Upvotes: 9

Related Questions