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.readli
why not try opening your file as text?
with open(fname, 'rt') as f:
lines = [x.strip() for x in f.readlines()]
Additionally here is a link for python 3.x on the official page: https://docs.python.org/3/library/io.html And this is the open function: https://docs.python.org/3/library/functions.html#open
If you are really trying to handle it as a binary then consider encoding your string.
Like it has been already mentioned, you are reading the file in binary mode and then creating a list of bytes. In your following for loop you are comparing string to bytes and that is where the code is failing.
Decoding the bytes while adding to the list should work. The changed code should look as follows:
with open(fname, 'rb') as f:
lines = [x.decode('utf8').strip() for x in f.readlines()]
The bytes type was introduced in Python 3 and that is why your code worked in Python 2. In Python 2 there was no data type for bytes:
>>> s=bytes('hello')
>>> type(s)
<type 'str'>
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'
.
Use encode() function along with hardcoded String value given in a single quote.
Ex:
file.write(answers[i] + '\n'.encode())
OR
line.split(' +++$+++ '.encode())
You opened the file in binary mode:
The following code will throw a TypeError: a bytes-like object is required, not 'str'.
for line in lines:
print(type(line))# <class 'bytes'>
if 'substring' in line:
print('success')
The following code will work - you have to use the decode() function:
for line in lines:
line = line.decode()
print(type(line))# <class 'str'>
if 'substring' in line:
print('success')
I got this error when I was trying to convert a char (or string) to bytes
, the code was something like this with Python 2.7:
# -*- coding: utf-8 -*-
print( bytes('ò') )
This is the way of Python 2.7 when dealing with unicode chars.
This won't work with Python 3.6, since bytes
require an extra argument for encoding, but this can be little tricky, since different encoding may output different result:
print( bytes('ò', 'iso_8859_1') ) # prints: b'\xf2'
print( bytes('ò', 'utf-8') ) # prints: b'\xc3\xb2'
In my case I had to use iso_8859_1
when encoding bytes in order to solve the issue.
Hope this helps someone.