Solving TypeError: a bytes-like object is required, not ‘str’ in Python and CSV

TypeError: a bytes-like object is required, not ‘str’ is a common error which occurs when you are opening the csv file in binary mode in Python 3.

In this post we will try to solve your error and see why it occurs in the first place, we will present multiple solutions so you can find the one which suits your case.

Describing TypeError: a bytes-like object is required, not ‘str’ in Python and CSV

The error usually happens when developers confuse the syntax of Python 2 and Python 3, since in Python 2 we take the input in binary mode but in Python 3 it is taken in text mode.

To replicate the error, we use the line of code bellow in order to open the csv file.

                                                                       #
outfile=open('./data.csv','wb')
                                                                       #

But we get the error bellow instead

                                                                       #
TypeError: a bytes-like object is required, not 'str' in python
                                                                       #

This is the content of the file

                                                                       #
CASA ,54,495435776
RAT ,56,13435611
TANJ,56,3141345372
TET,46,104455637
ASLY,39,43514965
ALMA,56,145435723
FARKA,4,6038458
                                                                       #

Solution : Use ‘w’ instead of ‘wb’ or the Opposite

If you are in Python 3 you should use w instead of wb

So if you are opening the csv with bellow code, that code should be changed by the second line of code bellow.

                                                                       #
outfile=open('./data.csv','wb')
                                                                       #

Should be replaced by

                                                                       #
outfile=open('./data.csv','w')
                                                                       #

The error can also occur if you are on Python 2 and using the syntax of Python 3 without even noticing it.

So in this case you should reverse the replacement and replace w with wb instead. So

                                                                       #
outfile=open('./data.csv','w')
                                                                       #

Becomes

                                                                       #
outfile=open('./data.csv','wb')
                                                                       #

I hope the solutions have been helpful, I hope you solved the error already, you can support us by donating to our Kofi account, this website is free to use but any help is very appreciated and goes a long way.

Summing-up

The error above can be very confusing especially if you copied a snippet of code that uses Python 2 syntax and tried to use it in a Python 3 script.

The fix is simple and works for most cases, thank you for reading and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/