2024-09-29 21:45:03 发布
网友
我有一个奇怪的问题: 我有来自串行端口的字节数据。 我用以下方法解码:
data.decode("utf8","backslashreplace")
输出如下所示:
\xf028B |\x80 10:00p@ @+\x81 :0000
就像我想要的那样。 但是当我过滤正则表达式时(在regexr.com)你知道吗
data = re.search('(?<=\\x80).{10}?',data)
只是找不到匹配的。输出为“无”。你知道吗
我错过什么了吗?你知道吗
使用您的代码,它会产生您期望的结果。比较:
[GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> import re >>> print(sys.version) 3.7.0 (default, Aug 22 2018, 20:50:05) [GCC 5.4.0 20160609] >>> data = '\xf028B |\x80 10:00p@ @+\x81 :0000' >>> pattern = '(?<=\\x80).{10}?' >>> print(re.search(pattern, data).group()) 10:00p@ @ >>>
屏幕盖: 如果您运行的是Python3.7windows,可能会有所不同?
更新:也适用于在Windows 10 64位上运行的Python3.7.0:
(53734290) C:\Users\daedw\Stackoverflow\53734290>Scripts\python.exe Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> import re >>> print(sys.version) 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] >>> data = '\xf028B |\x80 10:00p@ @+\x81 :0000' >>> pattern = '(?<=\\x80).{10}?' >>> print(re.search(pattern, data).group()) 10:00p@ @ >>>
windows屏幕帽:
使用您的代码,它会产生您期望的结果。比较:
屏幕盖:
如果您运行的是Python3.7windows,可能会有所不同?更新:也适用于在Windows 10 64位上运行的Python3.7.0:
windows屏幕帽:
相关问题 更多 >
编程相关推荐