• Register
0 votes
1.2k 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

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

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 5 views
5 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 8 views
8 views
Problem: after upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Mar 16 ummesalma 24.5k points
0 votes
1 answer 5 views
5 views
Problem: Can anyone help me out?
asked Mar 26 niyatiry 2.9k points
0 votes
1 answer 23 views
23 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 27 views
27 views
Problem: I am trying to connect to a server using a socket module in Python. However, I am getting an error like below. TypeError: a bytes-like object is required, not 'str' Here is my code: import socket HOST = '0.0.0.0' PORT = 12345 server_socket = socket.socket ... is causing this TypeError? and what should I do to fix it? I'm also open to any advice on how to improve your Python socket coding.
asked Feb 7 sasha 16.2k points
0 votes
1 answer 21 views
21 views
Problem: how to I Solve this: typeerror: a bytes-like object is required, not 'int' HELP! I need help understanding this: typeerror: a bytes-like object is required, not 'int' Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 8 Mashhoodch 13k points
0 votes
1 answer 2.5K views
2.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
0 votes
1 answer 254 views
254 views
Problem: i have json file named "panamaleaks50k.json" . I want to get ['text'] field from json file but it shows me following error the JSON object must be str, bytes or bytearray, not 'TextIOWrapper' this is my code with open('C:/Users/bilal butt/Desktop/PanamalEakJson.json','r') as lst: b = json.loads(lst) print(b['text'])
asked Feb 18 Mashhoodch 13k points
0 votes
1 answer 25 views
25 views
Program: I having trouble passing a function as a parameter to another function. This is my code: ga.py: def display_pageviews(hostname): pageviews_results = get_pageviews_query(service, hostname).execute() if pageviews_results.get('rows', []): pv = pageviews_results.get('rows') ... ='7daysAgo', end_date='today', metrics='ga:pageviews', sort='-ga:pageviews', filters='ga:hostname==%s' % hostname,)
asked Jan 11 Mashhoodch 13k points
0 votes
1 answer 307 views
307 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, 2020 alecxe 7.5k points