Python中的多字符串匹配

Python中的多字符串匹配,python,regex,string-matching,Python,Regex,String Matching,我正在编写一个自动化脚本来提取与日志文件中的第一个字符串(inputName)匹配的行,如果在该行中找到了特定的匹配项,则在该特定行中搜索第二个字符串(successful_msg),该行表示“文件已成功上载” 代码如下: import re successful_msg="file has been uploaded" def log_check(fileName): search_fileName = fileName print search_fileName

我正在编写一个自动化脚本来提取与日志文件中的第一个字符串(inputName)匹配的行,如果在该行中找到了特定的匹配项,则在该特定行中搜索第二个字符串(successful_msg),该行表示“文件已成功上载”

代码如下:

import re

successful_msg="file has been uploaded"
def log_check(fileName):
     search_fileName = fileName
     print search_fileName
     with open("/tmp/test.log") as line:
         for match in line:
                 m=re.search(r"%s" %search_fileName, match)
                 n=re.search(r"%s" %successful_msg,match)
                 if m and n:
                      print match
                 elif m:
                      print "File not updated"
                 else:
                      print "File is not processed"

 for inputName in glob.glob('./files/*'):
    log_check(inputName)
我能够从“if m and n:”行中获得成功消息。但是如果我包括“else”,我只看到“File is not processed”,即使第一个if通过了。逻辑哪里出了问题

例如:
ls文件/

abc-15  abc-16  abc-123  gg
我期望的输出应该是:

abc-15 
2015-03-17 06:09:26.122  INFO --- *** : The /tmp/test/abc-15 file has been uploaded
abc-16
2015-03-17 06:08:42.692  INFO --- *** : The /tmp/test/abc-16 file has been uploaded
gg
File is not processed
abc-123
File not updated
在循环中未注释/考虑else时的实际结果是:

gg
File not updated
abc-15
File not updated
abc-16
File not updated
abc-123
File not updated
当else被注释时,结果是:

gg
abc-15
2015-03-17 06:09:26.122  INFO ---*** : The /tmp/test/abc-15 file has been uploaded
abc-16
2015-03-17 06:08:42.692  INFO --- *** : The /tmp/test/abc-16 file has been uploaded
abc-123

我建议对您的
def
进行以下更改,并附上我的意见:

import re

successful_msg="file has been uploaded"
def log_check(fileName):
     search_fileName = fileName
     print search_fileName
     with open("/tmp/test.log") as line:
         # New variable to determine if fileName is matched or not
         matched = 0
         for match in line:
                 m = re.search(r"%s" %search_fileName, match)

                 # If fileName found, update above variable
                 if m:
                      matched = 1

                 n=re.search(r"%s" %successful_msg,match)

                 # On any line, if both phrases are present on the same line:
                 if n and m:
                      print match
                      break

         # If for loop exited without break...
         else:
              # If we found the filename...
              if matched == 1:
                  print "File not updated"
              # If even the filename was not found...
              else:
                  print "File is not processed"



for inputName in glob.glob('./files/*'):
    log_check(inputName)

如果文件的第一行没有您的
inputName
,您的
for
循环将停止,这真的是您所期望的吗?不,它将遍历所有行并打印匹配的行。但是,如果在日志文件中找不到任何inputName,我需要仅为该特定inputName打印“文件未处理”。请添加您的示例test.log。代码看起来没问题。我不太理解
elif m:print“File not updated”
(虽然我现在理解了问题的另一部分),那么在什么情况下应该打印它呢?场景是,当检测到文件时,将上载一些文件。可能会检测到文件,但可能由于任何问题而无法上载。因此,我想跟踪检测到并上传、检测到并没有上传、没有检测到的文件。以下是日志文件中的格式:2015-03-18 06:34:24.820信息--xxxxxxx:检测到/tmp/test/abc-16 2015-03-18 06:36:44.030信息--xxxxxxx:已检测到/tmp/test/abc-16文件uploaded@Yadunandana不客气!请将答案标记为已接受,并将您的问题标记为已解决:)