How To Fix “UnicodeEncodeError: ‘ascii’ codec can’t encode character in position” Error In Python

UnicodeEncodeError: ‘ascii’ codec can’t encode character in position

Some of the ways in this article will help you to fix the “UnicodeEncodeError: ‘ascii’ codec can’t encode character in position” error in Python. Follow these instructions to fix it.

What causes the “UnicodeEncodeError: ‘ascii’ codec can’t encode character in position” error?

The “UnicodeEncodeError: codec ‘ascii’ cannot encode characters in position” error occurs when using codec ‘ascii’ to encode characters in a string whose characters are not in the ‘ascii’ encoding.

Example:

characters = '  βµ∞ '
myCharacters = characters.encode('ascii')
print(myCharacters)

Output:

The program fails because the characters ‘βµ∞’ is not in the ‘ascii’ encoding.

Solutions to fix UnicodeEncodeError: ‘ascii’ codec can’t encode character in position” error

Using the utf-8 encoding

UTF-8 is an encoder that can encode more than ‘ascii’ (about a million characters are legally encoded in Unicode). So it will quickly solve this problem.

Example:

characters = '  βµ∞ '
myCharacters = characters.encode('utf-8')
print(myCharacters)

Output:

b'  \xce\xb2\xc2\xb5\xe2\x88\x9e '

Using “errors=’ignore’” to ignore unencoded characters while still using ‘utf-8’

Example:

  • Create a string with special characters.
  • Use the encode() function in conjunction with the ‘utf-8′ encoder and errors=’ignore’ to ignore unencoded characters in the utf-8 encoder.
  • Similarly, you can also decode strings with special characters using decode() with the same encode() function parameter.
# Encode "characters"
characters = ' βµ∞'
myCharacters = characters.encode('utf-8', errors='ignore')
print('Encode "characters":', myCharacters)  

# Decode "characters"
myCharactersAgain = myCharacters.decode('utf-8', errors='ignore')
print('Decode "characters":', myCharactersAgain)

Output:

Encode "characters": b' \xce\xb2\xc2\xb5\xe2\x88\x9e'
Decode "characters":  βµ∞

Solution 3: Using “errors=’ignore’” to ignore characters that are not in the ‘ascii’ encoding

Example:

  • Create a string with special characters.
  • Use the encode() function in conjunction with the ascii encoder and errors=’ignore’ to ignore unencoded characters in the ascii encoder.
  • Similarly, you can also decode strings with special characters using decode() with the same encode() function parameter.
# Encode "characters"
characters = "  john  βµ∞"
myCharacters = characters.encode("ascii", errors="ignore")
print('Encode "characters":', myCharacters)

# Decode "characters"
myCharactersAgain = myCharacters.decode("ascii", errors="ignore")
print('Decode "characters":', myCharactersAgain)

Output:

Encode "characters": b'  john  '
Decode "characters":   john

The program will ignore characters not in the ‘ascii’ encoding.

Install the code ‘UTF-8’ when your Python is not using it

If the characters in your Python file are being written in a certain code, you should change that code to ‘UTF-8’ so that Python can understand it. To install the ‘UTF-8’ encoder use the following command:

Syntax:

# coding : character code name

or

# coding = character code name

Example:

# Coding: shift_jis
print("สวัสดี")

Output:

สวัสดี

Note: สวัสดี is hello in Thai. To Python can decode it, you need to add the code # coding: shift_jis at the beginning of the ‘print’ statement. This method works for computers with non-English operating systems.

Summary

I hope you solved the “UnicodeEncodeError: ‘ascii’ codec can’t encode character in position” problem on your own through the examples. To read more good articles, visit learnshareit. Thanks!

Maybe you are interested in similar errors:

Leave a Reply

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