【问题标题】:Python - Print line based on partial stringPython - 基于部分字符串的打印行
【发布时间】:2021-12-31 22:33:22
【问题描述】:

我有 2 个文件要相互检查,然后取出它们所在的行。

我试图用正则表达式来做,但我一直收到这个错误,假设是因为我正在访问一个文件而不是直接显示一个字符串

File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\re.py", line 248, in finditer
    return _compile(pattern, flags).finditer(string)
TypeError: expected string or bytes-like object

这是我用于正则表达式搜索的内容

regex = r"\d(.*(10.0.0.0).*)"

with open('test1.txt', 'r') as file1:
    test = file1.read().splitlines()
    matches = re.finditer(regex, test)

    for matchNum, match in enumerate(matches, start=1):
        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

我也试过了,还是报错

with open("test1.txt") as file1, open("test2") as file2:
    st = set(map(str.rstrip,file1))
    for line in file2:
        spl = line.split(None, 1)[0]
        if spl in st:
            print(line.rstrip())

错误是

IndexError: list index out of range

我正在尝试将 IP 列表与路由器的输出相匹配,因此 test2 文件将如下所示

10.0.0.0/8
11.0.0.0/8
12.0.0.0/8
13.0.0.0/8

路由器输出看起来像

1 X test         10.0.0.0/8                          nov/19/2021 13:03:08
2 X test         11.0.0.0/8                          nov/19/2021 13:03:08
3 X test         12.0.0.0/8                          nov/19/2021 13:03:08
4 X test         13.0.0.0/8                          nov/19/2021 13:03:08

我希望路由器的整条线路仅与 ​​IP 匹配,而不必放置整个预期输出

希望这已经足够了,对这一切还是很陌生,干杯

【问题讨论】:

    标签: python file compare output string-matching


    【解决方案1】:

    完整答案是

    with open("router_output.txt") as file1, open("list_of_ips.txt") as file2:
        ips_to_keep = file2.read().splitlines()
        router_lines = file1.read().splitlines()
    
    ips_to_keep = [" " + ip + " " for ip in ips_to_keep]
    
    for line in router_lines:
        if any(ip in line for ip in ips_to_keep):
            print(line)
    
    
    
    

    假设您的文件有空格而不是制表符:)

    【讨论】:

      【解决方案2】:

      如果您有一个包含实际 IP 的文件,并且您不需要正则表达式,那么您可以这样做

      my_str = """
      1 X test         10.0.0.0/8                          nov/19/2021 13:03:08
      2 X test         11.0.0.0/9                          nov/19/2021 13:03:08
      3 X test         12.0.0.0/12                          nov/19/2021 13:03:08
      4 X test         13.0.0.0/2                          nov/19/2021 13:03:08
      5 X test         555.0.0.0/2                          nov/19/2021 13:03:08 #expecting this to not be printed
      """
      
      keep_ips = [' 10.0.0.0/8 ', ' 11.0.0.0/9 ', ' 12.0.0.0/12 ', ' 13.0.0.0/2 ']
      
      
      for line in my_str.split('\n'):
          if any(ip in line for ip in keep_ips):
              print(line)
      

      我在keep_ips 中添加了空格填充,否则您可以匹配113.0.0.0/25 之类的内容,因为它包含子字符串13.0.0.0/2

      您可以重构此代码以读取来自路由器的行,然后是 IP 行,在 IP 的任一端添加空格,然后使用此逻辑,使用 any

      我希望这对你现在有用

      【讨论】:

      • 您的问题是 regex = r"\d(.*(10.0.0.0).*)" 仅匹配其中包含 10.0.0.0 的内容,这是非常严格的。如果你想匹配172.12.3.215/24 之类的东西,那么你将不得不更聪明地使用你的正则表达式,并阅读如何匹配不同数量的可能数字。
      • 感谢您的建议。不幸的是,这不是我想要的,我想将 IP 保存在与脚本和路由器输出不同的文件中。我在此使用的 IP 也是一个示例,我将使用与此正则表达式不匹配的不同 IP(除非有一种方法可以从文件中导入正则表达式参数中的字符串)。还需要获取完整的行,因为我需要路由器分配的规则 ID,以便我可以自动执行操作(ID 是路由器输出左侧的编号)。如果您需要额外信息或完整脚本 lmk
      • 那么,你想基本上过滤包含1 X test 10.0.0.0/8 nov/19/2021 13:03:08 行的文件,所以只有来自test2 文件的IP 行吗?还是有点迷茫
      • 因此,您将在文件中创建 IP 列表,然后将这些 IP 与路由器的输出进行比较。如果有任何匹配,则打印来自路由器的完整行。因此,如果您正在检查 10.0.0.0,它将打印完整的 1 X test 10.0.0.0/8 nov/19/2021 13:03:08
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2023-03-19
      • 1970-01-01
      • 2022-11-30
      • 2011-10-22
      • 2019-10-03
      相关资源
      最近更新 更多