pythonnon-ascii-characterspython-unicodenon-unicode

UnicodeEncodeError: 'ascii' codec can't encode characters due to één from database


I have a field to get from database which contains string with this part één and while getting this i get error:

"UnicodeEncodeError: 'ascii' codec can't encode characters in position 12-15: ordinal not in range(128)"

I have search this error, and other people were having issue due to unicodes which start something like this u'\xa0, etc. But in my case, i think its due to special characters. I can not do changes in database as its not under my access. I can just access it.

The code is here: (actually its call to external url)

req = urllib2.Request(url)
req.add_header("Content-type", "application/json")
res = urllib2.urlopen(req,timeout = 50)         #50 secs timeout
clientid = res.read()
result = json.loads(clientid)

Then I use result variable to get the above mentioned string and I get error on this line:

updateString +="name='"+str(result['product_name'])+"', "

Solution

  • You need to find the encoding for which is used for your data before it's inserted into the database. Let's assume it's UTF-8 since that's the most common.

    In that case you will want to UTF-8 decode instead of ascii decode. You didn't provide any code, so I'm assuming you have "data".decode(). Try "data".decode("utf-8"), and if your data was encoded using this encoding, it will work.