How To Solve TypeError: encoding without a string argument in Python

TypeError: encoding without a string argument in Python

TypeError: encoding without a string argument in Python” is a common error related to the bytes encoding to files. Using for loop is one of those solutions you can use to fix this error. Let’s go into detail now.

How does the “TypeError: encoding without a string argument” in Python occur?

This error occurs when you work with anything related to bytes, such as bytes() functions. If you are using the bytes() function and get the error, maybe you have passed a parameter that is not exactly a string to the function. 

For example, you have a bad function which calls bytes() on a number type instead of a string:

bytes(2022, encoding="charmap")

The function expects the string you want to convert to a byte sequence. If a None object is provided, this error will be raised:

bytes(None, encoding="charmap")

To fix this error, we suggest you the following solutions:

How to solve the error?

Using str()

As we have explained, you must pass a string into the bytes() function. However, if you have passed a function call that you think will return a string, it may be the root of the problem. Because the function you called has encountered some cases it cannot handle (such as exception), an object None will be returned. To solve this, you must pass the function call inside the str() and pass that str() to the bytes() parameter:

def badFunction(a):
  return a

print(bytes(str(badFunction(2022)), encoding="charmap"))

Output

b'2022'

The above solution also works for None object (or any type) returned:

def badFunction(a):
  return a

print(bytes(str(badFunction(None)), encoding="charmap"))

Output

b'None'

The logic behind this method is understandable; we use str() to make sure to convert the result of your function call (badFunction) into a string. If your function call returns a string, the str() method won’t do anything, otherwise, if the result is a number or None object, then the str() will do its job.

Using for loop

Another way to fix this error is to use a for loop to iterate through all elements if you have a list or an array of strings instead of one.  

strings = ['LearnShareIT','2022']

for x in strings:
    print(bytes(x, encoding="charmap"))

Output

b'LearnShareIT'
b'2022'

However, remember that your list of strings (or array of strings) must only contain elements of string types only. If there exists a number or None object, the error will be raised:

strings = ['LearnShareIT',2022]

for x in strings:
    print(bytes(x, encoding="charmap"))

Output

b'LearnShareIT'
Traceback (most recent call last):
  File "main.py", line 11, in <module>
    print(bytes(x, encoding="charmap"))
TypeError: encoding without a string argument

To tackle this situation, you can use the function str(x) to make sure all elements will be converted to string type with the help of the str() function in Python.

strings = ['LearnShareIT',2022,None]

for x in strings:
    print(bytes(str(x), encoding="charmap"))

Output

b'LearnShareIT'
b'2022'
b'None'

Summary

In this tutorial, we have helped you to deal with the error “TypeError: encoding without a string argument” in Python. By checking the correct string type and using the str() function, you can easily tackle this problem. Good luck to you!

Maybe you are interested:

Leave a Reply

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