我有一个以字节形式在套接字上接收的变量,我需要检查是否包含特定字符串。
例如:
>>> a="foo\r\nbar"
>>> print(str(a.find("\r\n")))
3
这工作正常,但如果第一个变量
a
被转换为字节它将不再起作用。>>> a=b"foo\r\nbar"
>>> print(str(a.find("\r\n")))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
如果我转换变量
a
也不起作用再次作为字符串。>>> print(str(str(a).find("\r\n")))
-1
我如何检查
\r\n
在字节变量中?
最佳答案
>>> a = b"foo\r\nbar"
>>> print(a.find(b"\r\n"))
3
或者你可以像 TheGirrafish 所说的那样解码字符串
关于python - 如何检查特定字符串是否在字节变量的变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38777714/