Python 中的多个字符串匹配
Multiple string matching in Python
我正在编写一个自动化脚本来提取与日志文件中第一个字符串 (inputName) 匹配的行,如果在该行中找到该特定匹配项,则在该特定行中搜索第二个字符串 (successful_msg)它说 "file is uploaded successfully"。
下面是代码:
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",即使第一个 if 通过,我也只会看到 "File is not processed"。逻辑错在哪里?
例如:ls files/
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) 匹配的行,如果在该行中找到该特定匹配项,则在该特定行中搜索第二个字符串 (successful_msg)它说 "file is uploaded successfully"。
下面是代码:
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",即使第一个 if 通过,我也只会看到 "File is not processed"。逻辑错在哪里?
例如:ls files/
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)