分组并将文本输出到cs

2024-06-01 00:09:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从以下示例数据中查找设备地址、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?

Tags: thereisportaddressenabledeviceelement
1条回答
网友
1楼 · 发布于 2024-06-01 00:09:03

如果我正确地阅读了您的示例输入和输出,那么对于设备地址42端口3,实际上应该有6次失败的尝试。似乎我们可以得到一个失败的尝试延迟打印,即使它已经开始在下一个地址和端口。你知道吗

不管是哪种方式,下面的代码都有效,捕获了6次尝试,并忽略了前几次没有开始尝试的尝试。我正在使用字典将地址和端口的唯一组合映射到失败尝试的次数。如果这样的配对不是唯一的,但是您想区分同一对地址和端口的尝试,则需要在遇到每一对时添加唯一标识符。如果你对答案还有任何疑问,请告诉我。你知道吗

import os
import argparse
import operator
import re

def main():
    p = argparse.ArgumentParser (description="Creates a csv file report on data file.")
    p.add_argument("datafile", help="Path of data")
    p.add_argument("outfile", help="Path of new file.")
    args = p.parse_args()

    with open(args.datafile) as f:
        lines = f.read().splitlines()

    port_to_addr = dict()
    port_addr_to_count = dict()
    current_port = 'default'
    current_addr = 'default'
    for line in lines:
        usbPres = re.search("USB",line)
        if usbPres:
            devAddr = re.search("address\s(\d+)", line)
            beginEnumerate = re.search("enumerate", line)
            port = re.search("port\s(\d+)", line)

            if devAddr:
                current_addr = devAddr.group(1)
            elif beginEnumerate and port:
                current_port = port.group(1)
                port_to_addr[current_port] = current_addr
                port_addr_to_count[(current_port, port_to_addr[current_port])] = 1
            elif port:
                current_port = port.group(1)
                if current_port in port_to_addr and (current_port, port_to_addr[current_port]) in port_addr_to_count:
                    port_addr_to_count[(current_port, port_to_addr[current_port])] += 1

    header_row = ["DevicAddr", "Port", "Number of failed attempts"]
    rows = []
    for (port, addr) in port_addr_to_count:
        rows.append([addr, port, str(port_addr_to_count[(port, addr)])])

    rows.sort()
    rows.insert(0,header_row)

    newfile_lines = [",".join(row) + "\n" for row in rows]

    with open(args.outfile, 'w') as f:
        f.writelines(newfile_lines)


if __name__ == '__main__':
    main()

对示例数据运行后的输出是:

DevicAddr,Port,Number of failed attempts
42,3,6
47,7,3
52,7,2

相关问题 更多 >