我想从以下示例数据中查找设备地址、usb端口和出错次数:
以下是我目前掌握的情况:
import re
import csv
f = open("file.txt", "r")
searchlines = f.readlines()
f.close()
for element in searchlines:
usbPres = re.search('(USB)',element) #pattern to find usb lines
devAddr = re.search('(address)\s\d+',element) #parsing pattern for device address
port = re.search('(port)\s\d',element) #parsing pattern for port
if usbPres:
这就是我迷路的地方,因为我想为设备地址分配正确的端口,然后在将新设备插入该端口之前计算失败的时间,然后将其写入CSV文件。你知道吗
预期产量将为DevicAddr Port Number of failed attempts
42 3 5
47 7 2
52 7 1
“样本数据”
enter code here
[11883.112089] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11883.224080] usb 1-7: new high speed USB device using ehci_hcd and address 42
[11883.328151] hub 1-0:1.0: unable to enumerate USB device on port 3
[11904.472097] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11907.440096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11910.408093] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11913.376095] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11913.616090] usb 1-7: new high speed USB device using ehci_hcd and address 47
[11913.716121] hub 1-0:1.0: unable to enumerate USB device on port 7
[11927.340096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11930.308096] hub 1-0:1.0: Cannot enable port 7. Maybe the USB cable is bad?
[11933.276124] hub 1-0:1.0: Cannot enable port 7. Maybe the USB cable is bad?
[11934.224080] usb 1-7: new high speed USB device using ehci_hcd and address 52
[11936.244118] hub 1-0:1.0: unable to enumerate USB device on port 7 is bad?
[11939.212116] hub 1-0:1.0: Cannot enable port 7. Maybe the USB cable is bad?
如果我正确地阅读了您的示例输入和输出,那么对于设备地址42端口3,实际上应该有6次失败的尝试。似乎我们可以得到一个失败的尝试延迟打印,即使它已经开始在下一个地址和端口。你知道吗
不管是哪种方式,下面的代码都有效,捕获了6次尝试,并忽略了前几次没有开始尝试的尝试。我正在使用字典将地址和端口的唯一组合映射到失败尝试的次数。如果这样的配对不是唯一的,但是您想区分同一对地址和端口的尝试,则需要在遇到每一对时添加唯一标识符。如果你对答案还有任何疑问,请告诉我。你知道吗
对示例数据运行后的输出是:
相关问题 更多 >
编程相关推荐