Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error :cannot use a string pattern on a bytes-like object

Hy am using Python RegEx to show all internet wirless profiles connected to a computer.There is error (TypeError: cannot use a string pattern on a bytes-like object) in my Second last line pls anyone help to identifi my mistake.Thanks

My Program

import subprocess,re
command = "netsh wlan show profile"
output = subprocess.check_output(command, shell=True)  
network_names = re.search("(Profile\s*:\s)(.*)", output)  
print(network_names.group(0))

.....................................................

ERROR

line 8, in <module>


 return _compile(pattern, flags).search(string)


TypeError: cannot use a string pattern on a bytes-like object
like image 449
emma juila Avatar asked May 10 '20 23:05

emma juila


People also ask

How do you fix Cannot use string pattern on a bytes like object?

The Python "TypeError: cannot use a string pattern on a bytes-like object" occurs when we try to use a string pattern to match a bytes object. To solve the error, use the decode() method to decode the bytes object, e.g. my_bytes. decode('utf-8') .

How do you convert bytes to strings?

One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable. The simplest way to do so is using valueOf() method of String class in java.

WHAT IS RE sub in Python?

sub() function belongs to the Regular Expressions ( re ) module in Python. It returns a string where all matching occurrences of the specified pattern are replaced by the replace string. To use this function, we need to import the re module first.


Video Answer


2 Answers

Python 3 distinguishes "bytes" and "string" types; this is especially important for Unicode strings, where each character may be more than one byte, depending on the character and the encoding.

Regular expressions can work on either, but it has to be consistent — searching for bytes within bytes, or strings within strings.

Depending on what you need, there are two solutions:

  • Decode the output variable before searching in it; for instance, with: output_text = output.decode('utf-8')

    This depends on the encoding that you are using; UTF-8 is the most common these days.

    The matched group will be a string.

  • Search with bytes by adding a b prefix to the regular expression. A regular expression should also use the r prefix, so it becomes: re.search(br"(Profile\s*:\s)(.*)", output)

    The matched group will be a bytes object.

like image 195
Jiří Baum Avatar answered Sep 23 '22 16:09

Jiří Baum


From the documentation for Popen.stdout:

If the stdout argument was PIPE, this attribute is a readable stream object as returned by open(). Reading from the stream provides output from the child process. If the encoding or errors arguments were specified or the universal_newlines argument was True, the stream is a text stream, otherwise it is a byte stream. If the stdout argument was not PIPE, this attribute is None.

So without setting these options you get a byte stream.

subprocess.check_output supports an encoding keyword argument. Set this to 'utf8' and you will get a text stream:

output = subprocess.check_output(command, shell=True, encoding='utf8')
like image 23
drootang Avatar answered Sep 22 '22 16:09

drootang