【问题标题】:How to search for multiple strings in a file with multiline string如何在具有多行字符串的文件中搜索多个字符串
【发布时间】:2019-08-15 01:39:08
【问题描述】:

试图编写一个脚本,在整个文件中搜索某些字符串。

2 个以上的字符串。

1) 首先搜索是检查以下两行之一:

0/RP1/CPU0    RP(Active)

或者

0/RP0/CPU0    RP(Active)

如果是“0/RP1/CPU0 RP(Active)”,则打印此消息“execute command location 0/rp1/cpu0

如果是“0/RP0/CPU0 RP(Active)”,则打印此消息“execute command location 0/rp0/cpu0

2) 第二次搜索是检查以下多行之一: 一)

INFO_LINE------------------: TITLE_LINE(A-Z)
  State                              : ENABLED

b)

INFO_LINE------------------: TITLE_LINE(A-Z)
  State                              : DISABLE

TITLE_LINE(A-Z)”可能略有不同,但INFO_LINE 将是静态的,并且在ENABLEDDISABLE 中相同。

如果 b) 为真,则打印“restart process on location (FROM SEARCH1).

我已经尝试过if/else/elif 语句,并且一直在使用 re.search 进行正则表达式的研究。

#!/usr/bin/python
activerp = open('sample-output.txt')

def check_active_rp():
    for line in activerp:
        if line.find('0/RP1/CPU0    RP(Active)'):
           print("execute command location 0/rp1/cpu0")
        else: 
           if line.find('0/RP0/CPU0    RP(Active)'):
            print("execute command location 0/rp0/cpu0")

运行这个脚本 python 只会让我回到 cli 提示符,我无法进一步完成其他搜索。

CLI$ python test.py CLI$

【问题讨论】:

  • 你可能应该在这里使用regex,你也在全局范围内定义一个变量,然后在你的函数的本地范围内使用它,这是不好的做法,要么将其作为变量传递,要么在那里初始化它以避免其他可能改变该变量的东西

标签: python file search


【解决方案1】:

我想这就是你想要的:

def check_active_rp():
   string = '0/RP1/CPU0    RP(Active)'
   for line in activerp:
      if string in line:
         print('execute command location 0/rp1/cpu0')

【讨论】:

    【解决方案2】:

    我创建了一个包含您正在搜索的字符串的文件并进行了一些测试,您的示例应该会为您提供一些输出,尽管是错误的。这让我觉得你对 python 脚本没有完全掌握,但如果我错了,请纠正我。

    为了执行您的函数,您需要调用它。写def 只是简单地定义它。您可以在here找到更多相关信息。

    我看到您正在为此查看正则表达式,但如果您要搜索的字符串没有变体,则可以使用 find 函数。

    问题是line.find() 返回一个整数而不是布尔值。因此,您将始终输入第一个 if 语句,除非您的行以 '0/RP1/CPU0 RP(Active)' 开头(因为它将返回 0 索引)。如果我们检查documentation,我们可以看到如果没有找到字符串,find 函数会返回 -1。因此,您可以使用以下内容更改您的 if 语句:line.find('0/RP1/CPU0 RP(Active)') != -1。多行字符串也可以这样做。唯一的事情是您需要将整个文件转储到一个字符串中。因此,考虑到这一点,这是可以解决问题的解决方案。

    def check_active_rp(activerp):
        whole_file = activerp.read()
    
        if whole_file.find('0/RP1/CPU0    RP(Active)') != -1:
            print("execute command location 0/rp1/cpu0")
        elif whole_file.find('0/RP0/CPU0    RP(Active)') != -1:
            print("execute command location 0/rp0/cpu0")
    
        if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)\n  State                              : ENABLED') != -1:
            print('state is ENABLED')
        elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)\n  State                              : DISABLE') != -1:
            print('restart process on location (FROM SEARCH1)')
    
    
    with open('sample-output.txt') as active_rp:
        check_active_rp(active_rp)
    
    

    在您的示例中,您也永远不会关闭文件,因此我使用了 with 语句,这在处理 IO 时被认为是一种很好的做法。

    更新:

    我刚刚发现您想更改 info 行中的内容,在这种情况下使用正则表达式是合适的。然后以下解决方案将起作用:

    import re
    
    def check_active_rp(activerp):
        iterator = iter(activerp)
        for line in iterator:
            if line.find('0/RP1/CPU0    RP(Active)') != -1:
                print("execute command location 0/rp1/cpu0")
            elif line.find('0/RP0/CPU0    RP(Active)') != -1:
                print("execute command location 0/rp0/cpu0")
    
            pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')
    
            x = pattern.search(line)
    
            if x:
                line = next(iterator)
                if line.find('ENABLED') != -1:
                    print('the {} is ENABLED'.format(x.group(1)))
                elif line.find('DISABLE') != -1:
                    print('the {} is DISABLED'.format(x.group(1)))
    
    
    

    所以我们从文件中创建了一个迭代器并开始逐行遍历文件。我们仍然使用字符串查找函数进行第一次字符串搜索。现在我们继续到 INFO LINE。使用 regex 包,我们编译了一个捕获 TITLE_LINE 的正则表达式。一旦找到,我们从迭代器中获取下一行,并再次检查字符串是否包含 ENABLED 或 DISABLE;并相应地打印。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2011-02-04
      • 1970-01-01
      • 2020-06-25
      • 2012-05-17
      相关资源
      最近更新 更多