Most of the time the TypeError happens while doing the programming in Python but did you ever face typeerror: a bytes-like object is required not str?
A TypeError is for the most part raised when a specific operation is applied to an object of a wrong type.
The error” type error: a bytes-like object is required, not ‘str’” is raised when you treat an item as a string rather than as a progression of bytes.
The TypeError exemption shows that the operation you are attempting to execute isn’t upheld or not intended to be.
You will generally get this error when you pass contentions of some wrong type.
4 Ways to Solve TypeError: a bytes-like object is required, not str
Python requires that the input be an object with bytes-like properties, not just strings.
Python is a great programming language for beginners.
It’s simple, intuitive and easy to learn with its powerful features like dynamic typing or garbage collection!
However one thing that may surprise you unlike many other languages such as C/C++ where bytes are required instead of str.
They’re not optional either (you can’t store any data into these).
There can be various reasons that lead to the occurrence of TypeError.
A portion of these reasons are:
Attempting to perform an unsupported activity between two kinds of objects.
Trying to call a non-callable caller.
Attempting to emphasize over a non-iterative identifier.
We opened the file as:
with open('file_sample.txt', 'rb').
Here rb signifies binary mode which implies that all the data being perused from the document is returned as bytes.
Subsequently, when you take a look at line 6, you will find that we are attempting to part a byte object utilizing a string.
This activity isn’t allowed and prompts a TypeError.
Convert Bytes object
The easiest solution for our concern is to ensure that the object types match by changing over the delimiter string inside the split() capacity to a byte object.
You can accomplish this by utilizing the prefix b before the delimiter string inside the split() work.
This permits you to work upon a byte object inside the split() work, along these lines keeping away from the Type Error.
with open('file_sample.txt', 'r') as f: lines = [x.strip() for x in f.readlines()] Available_things = False for line in lines: tmp = line.strip().lower() split_line = tmp.split(';') if split_line=='bike' and int(split_line)>0: Available_things = True print("Available Thing is? "+ str(Available_things))
Available Thing is? False
By using decode
In the for loop, we are contrasting the string with bytes and that is by and large where the code is coming up short.
So to overcome this, you can decode the bytes while adding them to the list utilizing the decode() function.
The decode() technique permits you to change over from one encoding plan, in which the contention string is encoded to another ideal encoding plan
By using encode
The encode() strategy in Python is utilized to encode the string, utilizing the predetermined encoding.
You can encode the delimiter string inside the split() work utilizing .encode()
Open the file in Text Mode
Another work-around to our concern is to open the document in text mode.
You can accomplish this by utilizing rt rather than RB inside the open() work. Helpful guide: here
with open('file_sample.txt', 'r') as f: lines = [x.strip() for x in f.readlines()] Available_things = True for line in lines: tmp = line.strip().lower() split_line = tmp.split(';') if split_line=='bike' and int(split_line)>0: Available_things = False print("Available Things are? "+ str(Available_things))
Read more: ValueError: invalid literal for int() with base 10
This means that you’re trying to use a string (a series of letters) as if it were a byte array (a series of numbers), which won’t work. To fix this, you’ll need to convert the string into a byte array (or vice versa).