• Register
0 votes
1.4k views

Problem :

I am new  to Py 3.5. This code was working properly in Python 2.7:

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

for line in lines:
    tmp = line.strip().lower()
    if 'some-pattern' in tmp: continue
    # ... code

After upgrading to 3.5, I'm getting the:

TypeError: a bytes-like object is required, not 'str'
6.9k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

It seems you opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data is read from the file  returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'.

Further Readings:

https://github.com/mitsuhiko/phpserialize/issues/15

38.6k points
edited by
0 votes

Solution:

You opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'.

You can get rid of the error by using the below-mentioned way:-

And the reason you are getting that error is in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes strings instead. So... a couple of suggestions:

  1. The first thing you can use is c.sendall() method instead of c.send() to prevent possible issues where you may not have sent the entire msg with one call.

  2. For literals, add a 'b' for bytes string: c.sendall(b'Thank you for connecting')

  3. For variables, you need to encode Unicode strings to byte strings (see below)

Below is the code which will work both on Python 2.0 as well as Python 3.0:-
output = 'Thank you for connecting'

c.sendall(output.encode('utf-8'))

 

31.7k points

Related questions

0 votes
1 answer 43 views
43 views
Problem: Any help to solve this error: python typeerror: a bytes-like object is required, not 'str'
asked Mar 15 Wafa Abu Yousef 6.1k points
0 votes
1 answer 22 views
22 views
Problem: after upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Mar 16 ummesalma 29.1k points
0 votes
1 answer 9 views
9 views
Problem: Bytes-like object is required not 'str'
asked Jul 22 asha 121k points
0 votes
1 answer 16 views
16 views
Problem: Can anyone help me out?
asked Mar 26 niyatiry 3.9k points
0 votes
1 answer 24 views
24 views
Problem: I'm not sure how I got this : A bytes-like object is required not 'str' python.
asked May 24 Florina Gulnar 106k points
0 votes
1 answer 9 views
9 views
Problem: A bytes-like object is required not str
asked Jul 22 asha 121k points
0 votes
1 answer 11 views
11 views
Problem: Typeerror: a bytes-like object is required, not 'str.
asked Jun 26 Aurelia Maja 78.4k points
0 votes
1 answer 41 views
41 views
Problem: I've very recently migrated to Py 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp: continue # ... code After upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Jan 21 Mashhoodch 13k points
0 votes
1 answer 9 views
9 views
Problem: Python a bytes-like object is required, not 'str'?
asked Jun 30 Mezbah 130k points
0 votes
1 answer 12 views
12 views
Problem: Python a bytes-like object is required?
asked Jul 23 Nahil 128k points