• Register
0 votes
938 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 5 3
6,930 points

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

9 7 4
38,600 points
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'))

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 1.5K views
1.5K views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
1 vote
1 answer 56 views
56 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11 samhaz 5k points
0 votes
1 answer 198 views
198 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: “TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'” I am unable to figure out ... ) File "/Users/alecxe/project/Dev/project_files/project/main/management/commands/updatestats.py", line 34, in handle unique_visits=display_unique_visits(hostname),)
asked Jan 2 alecxe 7.5k points
1 vote
2 answers 85 views
85 views
Problem : The program supposed to take in two names, and if they are the same length it should check if they are the same word. But i am getting below error in python python typeerror: not all arguments converted during string formatting
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 9 views
9 views
On making a small program for math there returned an error "TypeError: 'NoneType' object is not subscriptable. Never before has been seen, this error, so no idea what it means. import math print("The format you should consider:") print str("value 1a")+str(" + ")+ ... /Nathan/Documents/Python/New thing", line 16, in <module> a = lista[1] - lista[0] TypeError: 'NoneType' object is not subscriptable
asked 8 hours ago psandprop 580 points