Unicodeencodeerror: 'Ascii' Codec Can't Encode Character U'\Xa0' in Position 20: Ordinal Not in Range(128)

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

You 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()

or work entirely in unicode.

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

Can you try

df= pd.read_csv('file_name.csv',encoding ='latin1')
or changing the encoding to utf8

Python UnicodeEncodeError: 'ascii' codec can't encode character in position 0: ordinal not in range(128)

I will answer my own question. Found an duplicated question. stackoverflow.com/questions/9942594/

But for simplicity, here is an elegant solution that works well with my use case:

def safe_str(obj):
try: return str(obj)
except UnicodeEncodeError:
return obj.encode('ascii', 'ignore').decode('ascii')
return ""

safe_str(u'\u2013')

Or simply use:

u'\u2013'.encode('ascii', 'ignore')

UnicodeEncodeError: 'ascii' codec can't encode character in position 0-4: ordinal not range(128)

server.sendmail('email.com', 'email.com', 'привет'.encode('utf-8'))

how to encode character '\xa0' in 'ascii' codec

You said you want to avoid the error, but how you avoid it matters.

Your title says you want to encode something to ASCII, but the thing you want to encode is not encodable in ASCII. There is no A0 character in 7-bit ASCII. You've asked the impossible.

You can decide among a few different things:

  • Encode with a lossy encode() parameter that says to throw away everything that doesn't fit in ASCII. This is dangerous and probably not very smart. If you can't trust your data, then why are you using your data?
  • Use a different encoding for output. You seem to know what encoding your text was, because you could fetch it and render it to Unicode. (OR, you are using ancient Python 2, and the default system encoding understands that page's encoding, and there's a silent .decode(DEFAULT_ENCODING) right before your .encode("ascii") . This is by far the best scheme. Just don't use ASCII. UTF-8 is the present and future!
  • Specifically snip out A0 with .replace() before your .encode(). Also pretty bad.
  • Get your page author to agree it should be ASCII and get himher to fix it. This is best of all.

`ascii‘ codec can’t enforce character u’\u2022‘ in position 206: ordinal not in range (128)

You should read the Python Unicode HOWTO. This error is the first example.

Basically, you shouldn't str to convert from Unicode to encoded text/bytes.

Instead, use .encode() to encode the string:

text = 'some_str'.encode('utf-8').strip()


Related Topics



Leave a reply



Submit