[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:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa1' 
in position 0: ordinal not in range(128) 
click below button to copy the code. By - python tutorial - team
  • 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:

a = u'\xa1'
print str(a) # this throws an exception 
click below button to copy the code. By - python tutorial - team

Results:

Traceback (most recent call last):
  File "unicode_ex.py", line 3, in 
    print str(a) # this throws an exception
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa1' in position 0: ordinal not in range(128)

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:
	import sys sys.getdefaultencoding()	
 
click below button to copy the code. By - python tutorial - team
  • 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:

import sys; reload(sys); sys.setdefaultencoding("utf8")	 
click below button to copy the code. By - python tutorial - team

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:
p.agent_info = u' '.join((agent_contact, agent_telno)).encode('utf-8').strip()
 
click below button to copy the code. By - python tutorial - team
  • Or work entirely in unicode.

Solution 4:

  • After googling around we figured the following and it helped. python 2.7 is in use.
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8') 
click below button to copy the code. By - python tutorial - team

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:
# 'value' contains the problematic data
unic = u''
unic += value
value = unic 
click below button to copy the code. By - python tutorial - team
 python unicode encode error

Learn python - python tutorial - python unicode encode error - python examples - python programs


Related Searches to UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)