Skip to main content
language highlight
Source Link
cottontail
  • 25.6k
  • 25
  • 184
  • 176

I've very recently migrated to Python 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 
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 

But in 3.5, on the if 'some-pattern' in tmp: continue line, I get an error which says:

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

I was unable to fix the problem using .decode() on either side of the in, nor could I fix it using

 if tmp.find('some-pattern') != -1: continue 
 if tmp.find('some-pattern') != -1: continue 

What is wrong, and how do I fix it?

I've very recently migrated to Python 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 

But in 3.5, on the if 'some-pattern' in tmp: continue line, I get an error which says:

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

I was unable to fix the problem using .decode() on either side of the in, nor could I fix it using

 if tmp.find('some-pattern') != -1: continue 

What is wrong, and how do I fix it?

I've very recently migrated to Python 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 

But in 3.5, on the if 'some-pattern' in tmp: continue line, I get an error which says:

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

I was unable to fix the problem using .decode() on either side of the in, nor could I fix it using

 if tmp.find('some-pattern') != -1: continue 

What is wrong, and how do I fix it?

edited title
Link
MisterMiyagi
  • 53.4k
  • 14
  • 131
  • 138

TypeError "TypeError: a bytes-like object is required, not 'str' after migrating to'str'" when handling file content in Python 3

fix title (the code was never *writing to* a file, but also there are many ways to create `bytes` data unexpectedly in 3.x so it's not that relevant); copyediting (ask explicitly)
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 133
  • 193

TypeError: a bytes-like object is required, not 'str' when writingafter migrating to a file in Python 3

I've very recently migrated to Python 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 toBut in 3.5, I'm gettingon the if 'some-pattern' in tmp: continue line, I get an error which says:

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

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

The error is onI was unable to fix the last line (the pattern search code).

I've triedproblem using the .decode() function on either side of the statement and also tried:in, nor could I fix it using

 if tmp.find('some-pattern') != -1: continue 

- to no avail.

I was able to resolve almost all Python 2-to-Python 3 issues quicklyWhat is wrong, but this little statement was bugging me.and how do I fix it?

TypeError: a bytes-like object is required, not 'str' when writing to a file in Python 3

I've very recently migrated to Python 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'

The error is on the last line (the pattern search code).

I've tried using the .decode() function on either side of the statement and also tried:

if tmp.find('some-pattern') != -1: continue 

- to no avail.

I was able to resolve almost all Python 2-to-Python 3 issues quickly, but this little statement was bugging me.

TypeError: a bytes-like object is required, not 'str' after migrating to Python 3

I've very recently migrated to Python 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 

But in 3.5, on the if 'some-pattern' in tmp: continue line, I get an error which says:

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

I was unable to fix the problem using .decode() on either side of the in, nor could I fix it using

 if tmp.find('some-pattern') != -1: continue 

What is wrong, and how do I fix it?

Second iteration.
Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
Expanded. Used more standard formatting. Kept the same verb tense in a sentence.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
edited tags
Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k
Loading
edited title
Link
jpp
  • 165.6k
  • 37
  • 301
  • 363
Loading
Question Protected by CommunityBot
edited title
Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k
Loading
edited tags
Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k
Loading
improved formatting
Source Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k
Loading
added 73 characters in body
Source Link
masroore
  • 10.2k
  • 4
  • 29
  • 31
Loading
Source Link
masroore
  • 10.2k
  • 4
  • 29
  • 31
Loading