How To Fix: “LookupError: unknown encoding” in Python

LookupError: unknown encoding in Python is not a rare error. Many developers get this LookupError when applying inappropriate encodings, which Python does not support. In this article, we will determine the causes and show the solutions. Read it and fix the error in your Python program now!

Cause of LookupError: unknown encoding in Python

You try to encode a string text. However, the code does not work and you receive the error message: LookupError: unknown encoding in Python.

Here is a code sample that leads to this LookupError:

# Create a string: "Learn Share It"  that needs to be encoded
encoderStr = "Learn Share It"

# Encode the string "Learn Share It"
encoderStr = encoderStr.encode('system encoding')
print (encoderStr)

An error message shows up like this:

    encoderStr = encoderStr.encode('system encoding')
LookupError: unknown encoding: system encoding

It is easy to understand, as you fill in the parameter of the .encode() function, an encoding that is not supported by Python. Since Python understands no “system encoding”, the LookupError will appear.

How to fix LookupError: unknown encoding in Python

As mentioned, you get the LookupError: unknown encoding in Python when using the encoding type, which Python does not support. Therefore, the solution for this issue is to use the correct type of supported encoding.

Python comes up with a list of Standard Encodings and Specific Encodings. You can visit the website of Python: 

https://docs.python.org/2/library/codecs.html#standard-encodings

to see which encoding types are supported. Apply these encodings in your Python program, and it will run properly.

Here is a code sample using the supported encoding (in the list of Standard Encoding):

# Create a string: "Learn Share It"  that needs to be encoded
encoderStr = "Learn Share It"

# Encode the string "Learn Share It"
encoderStr = encoderStr.encode('ascii')
print (encoderStr)

As you use the supported encoding type, the code will work properly to give the output:

b'Learn Share It'

On the other hand, you can also use encoding types in the list of Python Specific Encoding. Here is the code sample:

# Create a string: "Learn Share It"  that needs to be encoded
encoderStr = "Learn Share It"

# Encode the string "Learn Share It"
encoderStr = encoderStr.encode('raw_unicode_escape')
print (encoderStr)

The output will be:

b'Learn Share It'

If you want to use the Unicode encoding system, you can use utf-8 as the parameter of the .encode() function. Also, you can choose not to fill anything as the parameter of this function. In this case, the encoding is automatically set as utf-8.

See the sample below:

encoderStr = "The encoding is utf-8"

# Encode the string without filling parameter of encode()
encoderStr = encoderStr.encode()
print(encoderStr)

encoderStrUTF8 = "The encoding is also utf-8"

# Encode the string, with utf-8 filled in parameter of encode()
encoderStrUTF8 = encoderStrUTF8.encode("utf-8")
print(encoderStrUTF8)

The output will be:

b'The encoding is utf-8'
b'The encoding is also utf-8'

Summary

In conclusion, the LookupError: unknown encoding in Python occurs when the developers use the type of Python unsupported encoding types. The solution is to use the correct encodings in the list of Standard Encodings and Python Specific Encodings.

Leave a Reply

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