Unicodeencodeerror: 'Ascii' Codec Can't Encode Character at Special Name

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

Read the Python Unicode HOWTO. This error is the very first example.

Do not use str() to convert from unicode to encoded text / bytes.

Instead, 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 at special name

Try setting the system default encoding as utf-8 at the start of the script, so that all strings are encoded using that.

Example -

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

The above should set the default encoding as utf-8 .

“UnicodeEncodeError: 'ascii' codec can't encode character” in Python3

If your code is running within VS, then it sounds that Python can't work out the encoding of the inbuilt console, so defaults to ASCII. If you try to print any non-ASCII then Python throws an error rather printing text that won't display.

You can force Python's encoding by using the PYTHONIOENCODING environment variable. Set it within the run configuration for the script.

Depending on Visual Studio's console, you may get away with:

PYTHONIOENCODING=utf-8

or you may have to use a typical 8bit charset like:

PYTHONIOENCODING=windows-1252

UnicodeEncodeError: 'ascii' codec can't encode character

After investigating this some more I found out that I hadn't set the charset in my main Nginx config file:

http {
charset utf-8;
}

By adding the above, the problem disappeared and I think that this is the correct way of handling this issue.

How to deal with the 'ascii' codec can't encode character '\xe9' error?

I think this is a solution...

The problem is that the url you start with:

"https://www.elections.on.ca/content/dam/NGW/sitecontent/2022/results/Vote%20Totals%20From%20Official%20Tabulation%20-%20Orléans%20076.xlsx'

is already url-quoted (e.g. spaces replaced by %20), but still contains non-ascii chars here Orléans

So the solution from this question will help us, but just applying urllib.parse.quote(...) results in twice-encoded spaces as %2520. That is why you get a 404 when requesting the processed url.

So first we need to unquote the url (i.e. %20 ->> " "), then quote it again - this time the accented char will be quoted too and it should work.

Try this:

path = urllib.parse.quote(urllib.parse.unquote(link['href']))
url = "https://www.elections.on.ca" + path

The result we get is:

https://www.elections.on.ca/content/dam/NGW/sitecontent/2022/results/Vote%20Totals%20From%20Official%20Tabulation%20-%20Orl%C3%A9ans%20076.xlsx

...should work now!

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

Read the Python Unicode HOWTO. This error is the very first example.

Do not use str() to convert from unicode to encoded text / bytes.

Instead, 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?

Total repetition of your other question here (though here you finally deign to show us CODE from the start, wow!-). Answer is still identical: instead of

        ofile.write(Test + '\n')

do

        ofile.write(Test.encode('utf8') + '\n')

why do you keep repeating this Q, BTW?!



Related Topics



Leave a reply



Submit