-1

file1.txt 有以下几行:

[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender    
[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender    
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender    
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender 
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver  
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver  
[0]   0.00-34.53  sec  0.00 Bytes  0.00 bits/sec                  receiver 
[0]   0.00-34.75  sec  0.00 Bytes  0.00 bits/sec                  sender 

将以 [SUM] 开头并以 sender 和 receiver 结尾的行打印到另一个文本文件-file2.txt 中。

以下是代码:

with open(r"C:\Users\file1.txt", 'r') as f:

 contents = f.read()
 s=contents

def my_function1():

 regex = "^\s*\[SUM\]\s*[0-9\-\.]+\s+sec(?!\s+0\.00 Bytes).*sender.*"   
 items=re.findall(regex,s,re.MULTILINE)
 for y in items:
   file=open('file2.txt', "a")
   file.write(str(y))
   file.write("\n")
   file.close()


def my_function2():

 regex = "^\s*\[SUM\]\s*[0-9\-\.]+\s+sec(?!\s+0\.00 Bytes).*receiver.*"   
 items=re.findall(regex,s,re.MULTILINE)
 for y in items:
   file=open('file2.txt', "a")
   file.write(str(y))
   file.write("\n")
   file.close()
   #print(y)

my_function1()

my_function2()

它将 file2.txt 中的输出写为:

[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender    
[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender    
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender    
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender 
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver  
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver

预期:仅打印一次

[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver 
4

3 回答 3

1

您不需要这里的 re 模块,也不必将所有内容加载到内存中:

with open(r"C:\Users\file1.txt", 'r') as f, open('file2.txt', "w") as file:
    seen = set()     # use a set to only keep distinct lines
    for line in f:   # iterate the input file
        lr = line.rstrip()
        if line.startswith('one') and lr.endswith('apple'):
            if lr not in seen:
                seen.add(lr)
                _ = file.write(line)

如果搜索实际上更复杂并且需要re模块,我仍然会坚持一次处理一行并在循环之外编译正则表达式:

with open(r"C:\Users\file1.txt", 'r') as f, open('file2.txt', "w") as file:
    seen = set()     # use a set to only keep distinct lines
    rx = re.compile(pattern)
    for line in f:   # iterate the input file
        lr = line.rstrip()
        if rx.match(lr):
            if lr not in seen:
                seen.add(lr)
                _ = file.write(line)

如果您需要搜索 2 个模式并确保第一个匹配项在第二个匹配项之前写入,您可以使用:

patterns = ["^\s*\[SUM\]\s*[0-9\-\.]+\s+sec(?!\s+0\.00 Bytes).*sender.*",
            "^\s*\[SUM\]\s*[0-9\-\.]+\s+sec(?!\s+0\.00 Bytes).*receiver.*"]
rxs = [re.compile(pattern) for pattern in patterns]

with open(r"C:\Users\file1.txt", 'r') as f:
    data = [[], []]
    seen = set()     # use a set to only keep distinct lines
    for line in f:   # iterate the input file
        lr = line.rstrip()
        for i, rx in enumerate(rxs):
            if rx.match(lr):
                if lr not in seen:
                    seen.add(lr)
                    data[i].append(line)
with open('file2.txt', "w") as file:
    for lst in data:
        for line in lst:
            _ = file.write(line)
    print(file.getvalue())

它给出了预期:

[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender    
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender    
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver  
于 2020-04-03T12:59:53.983 回答
1

只需使用 awk:

$ awk '/^\[SUM]/ && !seen[$0]++' file
[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver

正如您所看到的,鉴于您发布的示例输入,您不需要像您想象的那样复杂的正则表达式,但如果您这样做了,那么也许这就是您正在寻找的东西(使用 GNU awk \s,其他 awk 使用[[:space:]]):

$ awk '/^\s*\[SUM]\s*[0-9.-]+\s+sec\s.*(sender|receiver)/ && !seen[$0]++' file
[SUM]   0.00-34.53  sec  2.11 GBytes   524 Mbits/sec                  sender
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender
[SUM]   0.00-34.62  sec  2.36 GBytes   586 Mbits/sec                  sender
[SUM]   0.00-34.75  sec  2.39 GBytes   591 Mbits/sec                  receiver
于 2020-04-03T12:55:13.677 回答
0

如果你想得到一个唯一的列表,只需 list(set(items)) 在写入文件之前添加:

于 2020-04-03T12:59:07.717 回答