Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How to fix typeerror: a bytes-like object is required, not 'str' in Python?
Next
Answered
Nora Evans
  • 23
Nora Evans
Asked: May 20, 20222022-05-20T08:08:50+00:00 2022-05-20T08:08:50+00:00In: python

How to fix typeerror: a bytes-like object is required, not ‘str’ in Python?

  • 23

. Advertisement .

..3..

. Advertisement .

..4..

Hey, geys! I am confused with the typeerror: a bytes-like object is required, not ‘str’ in Python. I am having the this file, I save it with the file_sample.txt name:

product;stock;price
bike;10;50
laptop;3;1000
microphone;5;40
book;3;9

Then I write the following program:

with open('file_sample.txt', 'rb') as f:
lines = [x.strip() for x in f.readlines()]

bike_available = False
for line in lines:
tmp = line.strip().lower()
split_line = tmp.split(';')
if split_line[0]=='bike' and int(split_line[1])>0:
bike_available = True

print("Bikes Available? "+ str(bike_available))

Here is the output:

Traceback (most recent call last):
Error line 7, in <module>
splitted_line = tmp.split(';')
TypeError: a bytes-like object is required, not 'str'

I have read a lot of documents, but I don’t know how to fix it. Does someone guide me to solve this error? Thanks!

a bytes-like object is required
  • 2 2 Answers
  • 71 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-12T01:25:29+00:00Added an answer on June 12, 2022 at 1:25 am

    The cause:

    You used with open('file_sample.txt', 'rb') to open the file. rb indicates that the file need to be opened in reading binary mode. Binary mode is reading the data in the form of bytes objects. If you examine line 7, you will notice that you are attempting to split a byte object with a string. Therefore, the operation returns a TypeError.

    Solution:

    To solve this error, the types which the split() operation uses need to match together. The best way is changing the delimiter to a byte object. I have successed by adding the string with b prefix as following:

    with open('file_sample.txt', 'rb') as f:
    lines = [x.strip() for x in f.readlines()]
    
    bike_available = False
    for line in lines:
    tmp = line.strip().lower()
    split_line = tmp.split(b';') # Prefixed the b 
    if split_line[0]==b'bike' and int(split_line[1])>0:
    bike_available = True
    
    print("Bikes Available? "+ str(bike_available))

    This is the output:

    Bikes Available? True
    

    I also prefixed the b  or added bytes conversion to the if split_line[0]==b'bike' condition, so the types also match, and the comparison is accurate.

    • 15
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Milo Brown
    2022-05-20T08:08:53+00:00Added an answer on May 20, 2022 at 8:08 am

    I removed the b from the open() action and the error disappeared.

    with open('file_sample.txt', 'r') as f:
    lines = [x.strip() for x in f.readlines()]
    
    bike_available = False
    for line in lines:
    tmp = line.strip().lower()
    split_line = tmp.split(';')
    if split_line[0]=='bike' and int(split_line[1])>0:
    bike_available = True
    
    print("Bikes Available? "+ str(bike_available))

    Output is:

    Bikes Available? True
    • 16
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.