我有一个.log文件,我想在其中检查是否有错误/警告:
2018-03-05 10:55:54,636 INFO The file: C:/test/Abu/TRS.ABU.GEN.003_1/input\ASWC_M740.aswc.arxml is well-formed
2018-03-05 10:55:55,193 INFO The file: C:/test/Abu/TRS.ABU.GEN.003_1/input\ASWC_M740.aswc.arxml is valid with the AUTOSAR4.2.2-STRICT schema
2018-03-05 10:55:55,227 INFO The file: C:/test/Abu/TRS.ABU.GEN.003_1/input\ASWC_M741.aswc.arxml is well-formed
2018-03-05 10:55:55,795 INFO The file: C:/test/Abu/TRS.ABU.GEN.003_1/input\ASWC_M741.aswc.arxml is valid with the AUTOSAR4.2.2-STRICT schema
2018-03-05 10:55:55,831 INFO The file: C:/test/Abu/TRS.ABU.GEN.003_1/input\ASWC_M742.aswc.arxml is well-formed
2018-03-05 10:55:56,403 INFO The file: C:/test/Abu/TRS.ABU.GEN.003_1/input\ASWC_M742.aswc.arxml is valid with the AUTOSAR4.2.2-STRICT schema
2018-03-05 10:55:56,438 WARNING ASWC_M740_MSI is without connector
2018-03-05 10:55:56,438 ERROR ASWC_M741_MSI is without connector
2018-03-05 10:55:56,438 WARNING PRP_CS_VehicleSPeed is without connector
到目前为止,我已经成功地编写了下一个函数,但没有成功:
def checkLog(path, level, message):
"""
path = used for defining the file to be checked
level = criticity level :INFO, WARNING, ERROR
message = string to be matched
"""
datafile = open(path)
line_file = datafile.readline()
while line_file != "":
for text in message:
if level + " " + text in line_file:
return True
line_file = datafile.readline()
return False
checkLog("C:\\test\Abu\TRS.ABU.GEN.003_1\output\\result.log", "WARNING", ["PRP_CS_VehicleSPeed", "ASWC_M740_MSI", "ASWC_M741_MSI"])
我错在哪里?你知道吗
我想你需要取消这行
line_file = datafile.readline()
。您当前所做的是检查第一行是否包含第一条消息,如果没有,则跳到第二行并检查它是否包含第二条消息。因此,它并不检查每一行是否都包含这三条消息中的一条。你知道吗我建议您为此使用
pandas
。这是一个说明性的例子。你知道吗设置
解决方案
示例查询
第二个
readline()
在for循环中,该循环对可能要匹配的消息进行迭代,因此在检查所有消息之前,代码将移到下一行。你知道吗尝试将其移到外部范围:
您的代码可以这样编写:
这避免了对
readline()
的调用,而不是对file对象进行迭代,从而简化了代码。此外,它还使用上下文管理器(with
语句)打开文件,这将确保文件正确关闭。你知道吗相关问题 更多 >
编程相关推荐