[Solved-5 Solutions] UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)
Error Description:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)
Solution 1:
- This error occurs when we pass a Unicode string containing non-English characters (Unicode characters beyond 128) to something that expects an ASCII bytestring.
- The default encoding for a Python bytestring is ASCII, "which handles exactly 128 (English) characters".
- This is why trying to convert Unicode characters beyond 128 produces the error.
- The good news is that we can encode Python bytestrings in other encodings besides ASCII.
- Django's smart_str function in the django.utils.encoding module, converts a Unicode string to a bytestring using a default encoding of UTF-8.
Here is an example using the built-in function, str:
Results:
Solution 2:
- It will try to read file in ascii resulting in all too common: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)
- After lots of trial and error we found a workaround that works. First of all check if we have this problem by executing:
- if it comes back with 'ascii' then read on.
- Default encoding need to be changed. However this is only possible when sys module is reloaded.
Here is a complete solution:
Solution 3:
- We need to read the Python Unicode HOWTO This error is the very first example ,
- Basically, stop using str to convert from unicode to encoded text / bytes.
- Instead, properly use .encode() to encode the string:
- Or work entirely in unicode.
Solution 4:
- After googling around we figured the following and it helped. python 2.7 is in use.
Solution 5:
- We just had this problem, and Google led me here, so just to add to the general solutions here, this is what worked for us:
Learn python - python tutorial - python unicode encode error - python examples - python programs