Error :cannot use a string pattern on a bytes-like object
Error :cannot use a string pattern on a bytes-like object
Hy am using Python RegEx to show all internet wirless profile 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
我的程序
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))
................................................ .........
错误
line 8, in <module>
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
我在 python 2.7 的计算机上尝试了相同的代码。
工作完美。
输出是我这边的str对象。
我想你可以在这段代码后加一行"output = subprocess.check_output(command, shell=True)",这行是print(type(output)).
你可能会看到真正的数据类型,如果不是str,尝试使用output = str(output)将其转换为str
来自 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.
因此,如果不设置这些选项,您将获得一个字节流。
subprocess.check_output
支持 encoding
关键字参数。将其设置为 'utf8'
,您将获得一个文本流:
output = subprocess.check_output(command, shell=True, encoding='utf8')
Python 3 区分“bytes" and "string”类型;这对于 Unicode 字符串尤其重要,其中每个字符可能超过一个字节,具体取决于字符和编码。
正则表达式可以在任何一个上工作,但它必须是一致的 — 在字节中搜索字节,或在字符串中搜索字符串。
根据您的需要,有两种解决方案:
在搜索之前解码 output
变量;例如,使用:output_text = output.decode('utf-8')
这取决于您使用的编码; UTF-8 是现在最常见的。
匹配的组将是一个字符串。
通过在正则表达式中添加 b
前缀来使用字节进行搜索。正则表达式也应该使用 r
前缀,所以它变成:re.search(br"(Profile\s*:\s)(.*)", output)
匹配的组将是一个字节对象。
Hy am using Python RegEx to show all internet wirless profile 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
我的程序
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))
................................................ .........
错误
line 8, in <module>
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
我在 python 2.7 的计算机上尝试了相同的代码。 工作完美。
输出是我这边的str对象。
我想你可以在这段代码后加一行"output = subprocess.check_output(command, shell=True)",这行是print(type(output)).
你可能会看到真正的数据类型,如果不是str,尝试使用output = str(output)将其转换为str
来自 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.
因此,如果不设置这些选项,您将获得一个字节流。
subprocess.check_output
支持 encoding
关键字参数。将其设置为 'utf8'
,您将获得一个文本流:
output = subprocess.check_output(command, shell=True, encoding='utf8')
Python 3 区分“bytes" and "string”类型;这对于 Unicode 字符串尤其重要,其中每个字符可能超过一个字节,具体取决于字符和编码。
正则表达式可以在任何一个上工作,但它必须是一致的 — 在字节中搜索字节,或在字符串中搜索字符串。
根据您的需要,有两种解决方案:
在搜索之前解码
output
变量;例如,使用:output_text = output.decode('utf-8')
这取决于您使用的编码; UTF-8 是现在最常见的。
匹配的组将是一个字符串。
通过在正则表达式中添加
b
前缀来使用字节进行搜索。正则表达式也应该使用r
前缀,所以它变成:re.search(br"(Profile\s*:\s)(.*)", output)
匹配的组将是一个字节对象。