What is bytes like object in python?

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.

What is bytes like object in python?

asked Oct 10, 2015 at 13:28

7

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'.

answered Oct 10, 2015 at 13:30

Martijn PietersMartijn Pieters

986k274 gold badges3877 silver badges3236 bronze badges

6

You can encode your string by using .encode()

Example:

'Hello World'.encode()

As the error describes, in order to write a string to a file you need to encode it to a byte-like object first, and encode() is encoding it to a byte-string.

answered May 22, 2016 at 16:17

4

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)

answered May 17, 2016 at 2:15

SureshSuresh

1,46510 silver badges7 bronze badges

1

You have to change from wb to w:

def __init__(self):
    self.myCsv = csv.writer(open('Item.csv', 'wb')) 
    self.myCsv.writerow(['title', 'link'])

to

def __init__(self):
    self.myCsv = csv.writer(open('Item.csv', 'w'))
    self.myCsv.writerow(['title', 'link'])

After changing this, the error disappears, but you can't write to the file (in my case). So after all, I don't have an answer?

Source: How to remove ^M

Changing to 'rb' brings me the other error: io.UnsupportedOperation: write

answered Apr 28, 2017 at 14:38

meck373meck373

1,0581 gold badge18 silver badges30 bronze badges

1

Use the encode() function along with the hardcoded string value given in a single quote.

Example:

file.write(answers[i] + '\n'.encode())

Or

line.split(' +++$+++ '.encode())

What is bytes like object in python?

answered Apr 20, 2019 at 8:47

Shiv BuyyaShiv Buyya

3,35928 silver badges24 bronze badges

1

For this small example, adding the only b before 'GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n' solved my problem:

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send(b'GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')

while True:
    data = mysock.recv(512)
    if (len(data) < 1):
        break
    print (data);

mysock.close()

What does the 'b' character do in front of a string literal?

What is bytes like object in python?

answered Mar 22, 2016 at 11:59

starterstarter

2012 silver badges2 bronze badges

1

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))# 
    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))# 
    if 'substring' in line:
       print('success')

answered May 16, 2018 at 7:23

Matan HugiMatan Hugi

9607 silver badges16 bronze badges

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: io — Core tools for working with streams.

And this is the open function: open

If you are really trying to handle it as a binary then consider encoding your string.

What is bytes like object in python?

answered Dec 1, 2017 at 12:22

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 characters.

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.

What is bytes like object in python?

answered May 5, 2020 at 13:56

Ibrahim.HIbrahim.H

9121 gold badge12 silver badges19 bronze badges

1

What is a bytes in Python?

Python bytes() We utilize the Python bytes() method to manipulate binary data in the program. The byte is a digital information unit that typically consists of 8 bits each of which consists of a 0 or 1. A byte is a computer architecture term for memory storage that encodes a single character of text in a computer.

How do you fix a bytes

To solve the Python "TypeError: a bytes-like object is required, not 'str'", encode the str to bytes, e.g. my_str. encode('utf-8') . The str. encode method returns an encoded version of the string as a bytes object.

What are byte strings in Python?

In Python, a byte string is just that: a sequence of bytes. It isn't human-readable. Under the hood, everything must be converted to a byte string before it can be stored in a computer. On the other hand, a character string, often just called a "string", is a sequence of characters. It is human-readable.

Does Python have byte?

Python bytes() Function It can convert objects into bytes objects, or create empty bytes object of the specified size. The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.