Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Split, A bytes-like object is required, not 'str'

I am trying to write a tshark (or any shell command for that matter) to a file. I've tried using decode and encode but it still yells at me that the split method cannot use the datatype. My attempts are still in the code as comments, after the "capturing stopped" line. I have also tried r, a and a+ as the open modes but I actually get output with the ab+ mode used here so I opted to keep it. Even using a+ mode said "blah" was bytes. I would like to keep the file appended with the output. Thank you!

import subprocess
import datetime

x="1"
x=input("Enter to continue. Input 0 to quit")
while x != "0":
    #print("x is not zero")
    blah = subprocess.check_output(["tshark -i mon0 -f \"subtype probe-req\" -T fields -e wlan.sa -e wlan_mgt.ssid -c 2"], shell=True)
    with open("results.txt", 'ab+') as f:
        f.write(blah)
    x=input("To get out enter 0")
print("Capturing Stopped")

# blah.decode()
#blah = str.encode(blah)

#split the blah variable by line
splitblah = blah.split("\n")

#repeat  for each line, -1 ignores first line since it contains headers
for value in splitblah[:-1]:

    #split each line by tab delimiter
    splitvalue = value.split("\t")

#Assign variables to split fields
MAC = str(splitvalue[1])
SSID = str(splitvalue[2])
time = str(datetime.datetime.now())

#write and format output to results file
with open("results.txt", "ab+") as f:
    f.write(MAC+" "+SSID+" "+time+"\r\n")
like image 854
axxic3 Avatar asked Jun 13 '18 05:06

axxic3


People also ask

How do you fix a bytes like an object is required not str?

Typeerror a bytes like object is required not str error occurs when we compare any 'str' object with the 'byte' type object. The best way to fix this error is to convert them into 'str' before comparison or any other operation.

Is a string a bytes like object?

Bytes-like object in python In Python, a string object is a series of characters that make a string. In the same manner, a byte object is a sequence of bits/bytes that represent data. Strings are human-readable while bytes are computer-readable. Data is converted into byte form before it is stored on a computer.

What is a bytes like object in Python?

Bytes-like objects are objects that are stored using the bytes data type. Bytes-like objects are not strings and so they cannot be manipulated like a string.

What is a bytes-like object is required Not Str?

The error “typeerror: a bytes-like object is required, not ‘str’” is raised when you treat an object as a string instead of as a series of bytes. A common scenario in which this error is raised is when you read a text file as a binary.

Why can’t I manipulate bytes-like objects?

Bytes-like objects are not strings and so they cannot be manipulated like a string. This error is commonly raised when you open a file as a binary file instead of as a text file.

Why does Python 3 throw TypeError-bytes-like object is required?

But the same code when executed in Python 3 will throw the error - typeerror: a bytes-like object is required, not 'str'. This is because, in Python 2, the strings are by default treated as bytes. The original strings in Python 2 are 8-bit strings, which play a crucial role while working with byte sequences and ASCII text.

How do I avoid a TypeError when using the split () function?

You can achieve this by using the prefix b before the delimiter string within the split () function. This allows you to operate upon a byte object within the split () function, thereby avoiding the TypeError. In our code, we are trying to read the file in binary mode and then creating a list of bytes.


2 Answers

If your question boils down to this:

I've tried using decode and encode but it still yells at me that the split method cannot use the datatype.

The error at hand can be demonstrated by the following code:

>>> blah = b'hello world'  # the "bytes" produced by check_output
>>> blah.split('\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

In order to split bytes, a bytes object must also be provided. The fix is simply:

>>> blah.split(b'\n')
[b'hello world']
like image 108
metatoaster Avatar answered Nov 15 '22 15:11

metatoaster


Use decode() correctly: either in two steps (if you want to reuse blah):

blah = blah.decode()
splitblah = blah.split("\n")
# other code that uses blah

or inline (if you need it for single use):

splitblah = blah.decode().split("\n")

Your issue with using decode() was that you did not use its return value. Note that decode() does not change the object (blah) to assign or pass it to something:

# WRONG!
blah.decode()

SEE ALSO:
decode docs.

like image 28
Timur Shtatland Avatar answered Nov 15 '22 15:11

Timur Shtatland