The reason for the type error can’t concatenate string to a byte object, when you try to bring together a Python string and a Python byte objects. You can solve this issue by either encoding your string or decoding your bytes objects and only then concatenate them.
Understanding the can’t concat str error message
Assume that you read data from a text or csv file into a Python string. As part of your processing, you try to join that string with a bytes object:
# read the contents of a file to a Python string from pathlib import Path file_path = Path(r"C:\Work_Dir\my_file.txt") with open (file_path, 'r') as f: file_str = f.read() # define a bytes object my_bytes = bytes( 'and this is a bytes object', 'windows-1255')
Let’s now concatenate the objects:
file_str + my_bytes
This will render the following typeerror exception – screenshot taken from Jupyter. You will get similar messages in PyCharm, VS Code, Spyder and others
You might as well get the following error:
typeerror: can't concat str to bytes
Solving the can only concat bytes type error
You can fix the error in several ways, here are a couple of ideas:
Convert string to bytes
You can cast your Python string as a bytes object and join it with other byte:
#Python3 my_byte_str = bytes (file_str, encoding='windows-1255') + my_bytes print(my_byte_str)
Note: When converting to bytes, make sure to define the encoding format (in our case – ‘windows-1255’) of your string.
Convert bytes to string
Other option is to do the opposite, that is to convert your bytes object to string. This could be a better option if you would like to take advantage of the string manipulation capabilities in Python.
#Python3 my_concat_str = file_str + str(my_bytes.decode()) print(my_concat_str)
Note: You might have encountered this error when combining two or more different columns of a pandas DataFrame. If so, you might want to use the following command to convert your pandas column / series from bytes to string and then concatenate the columns.