This content originally appeared on CodeSource.io and was authored by Deven
In this article, you will learn how to solve typeerror: a bytes-like object is required, not ‘str’ error in Python.
Let’s look at a code example that produces the same error.
with open("fruits.txt", "rb") as file:
fruits = file.readlines()
for r in fruits:
if "mango" in r:
print(r)
Output
TypeError: a bytes-like object is required, not 'str'
In order to solve typeerror: a bytes-like object is required, not ‘str’ error in Python we need to open fruits.txt
as read mode, in the above code snippet we tried to open txt file as binary. Consider the the correct code snippet below:
with open("fruits.txt", "r") as file:
fruits = file.readlines()
for r in fruits:
if "mango" in r:
print(r)
The post Solved – typeerror: a bytes-like object is required, not ‘str’ appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-03-12T05:02:36+00:00) Solved – typeerror: a bytes-like object is required, not ‘str’. Retrieved from https://www.scien.cx/2021/03/12/solved-typeerror-a-bytes-like-object-is-required-not-str/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.