How Do You Troubleshoot the Python Problem “UnicodeEncodeError: ‘ascii’ codec can’t encode character”?

Problem scenario
You are trying to print out a variable in Python. But you get an error like this: "UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 70567: ordinal not in range(128)"

What should you do?

Solution
Root cause: A variable needs to be encoded as UTF instead of the default ASCII encoding.

Procedures
Assuming that var1 is a variable holding the results of a requests.get(url), use these lines:

x = var1.text
print((x).encode('utf'))

Leave a comment

Your email address will not be published. Required fields are marked *