我有一个.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"])
我哪里错了?
答案 0 :(得分:2)
第二个readline()
位于for循环内,迭代可能匹配的消息,因此代码会在检查完所有消息之前移到下一行。
尝试将其移至外部范围:
def checkLog(path, level, message):
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
您的代码可以更好地编写:
def checkLog(path, level, message):
with open(path) as datafile:
for line in datafile:
for text in message:
if (level + " " + text) in line:
return True
return False
这样可以避免调用readline()
而不是遍历文件对象,从而简化了代码。它还使用上下文管理器(with
语句)打开文件,这将确保文件正确关闭。
答案 1 :(得分:0)
我建议您使用var IsIphoneX = (Ti.Platform.displayCaps.platformWidth === 375 && Ti.Platform.displayCaps.platformHeight === 812 && Ti.Platform.displayCaps.logicalDensityFactor === 3);
。这是一个说明性的例子。
<强>设置强>
pandas
<强>解决方案强>
import pandas as pd, numpy as np
from io import StringIO
mystr = StringIO("""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
""")
df = pd.read_csv(mystr, sep=',', header=None, names=['Timestamp', 'Message'])
示例查询
df['Message_Error'] = df.loc[df['Message'].str.contains('WARNING|ERROR'), 'Message'].apply(lambda x: x.split(' ')[:3])
df['Message_Error'] = df['Message_Error'].apply(lambda x: x if isinstance(x, list) else [])
df = df.join(pd.DataFrame(df['Message_Error'].values.tolist()))
# Timestamp Message \
# 0 2018-03-05 10:55:54 636 INFO The file: C:/test/Abu/TRS.ABU.GEN.003...
# 1 2018-03-05 10:55:55 193 INFO The file: C:/test/Abu/TRS.ABU.GEN.003...
# 2 2018-03-05 10:55:55 227 INFO The file: C:/test/Abu/TRS.ABU.GEN.003...
# 3 2018-03-05 10:55:55 795 INFO The file: C:/test/Abu/TRS.ABU.GEN.003...
# 4 2018-03-05 10:55:55 831 INFO The file: C:/test/Abu/TRS.ABU.GEN.003...
# 5 2018-03-05 10:55:56 403 INFO The file: C:/test/Abu/TRS.ABU.GEN.003...
# 6 2018-03-05 10:55:56 438 WARNING ASWC_M740_MSI is without connector
# 7 2018-03-05 10:55:56 438 ERROR ASWC_M741_MSI is without connector
# 8 2018-03-05 10:55:56 438 WARNING PRP_CS_VehicleSPeed is without con...
# Message_Error 0 1 2
# 0 [] None None None
# 1 [] None None None
# 2 [] None None None
# 3 [] None None None
# 4 [] None None None
# 5 [] None None None
# 6 [438, WARNING, ASWC_M740_MSI] 438 WARNING ASWC_M740_MSI
# 7 [438, ERROR, ASWC_M741_MSI] 438 ERROR ASWC_M741_MSI
# 8 [438, WARNING, PRP_CS_VehicleSPeed] 438 WARNING PRP_CS_VehicleSPeed
答案 2 :(得分:0)
我认为你需要取消这一行line_file = datafile.readline()
。你目前正在做的是检查第一行是否包含第一条消息,如果没有跳转到第二行并检查它是否包含第二条消息。因此,不检查每一行是否包含这三个消息中的一个。
答案 3 :(得分:-1)
您正在创建一个文件指针,但没有迭代它,因此您无法解析整个文件。
我建议使用上下文后卫和
with open(path, 'r') as datafile:
all_lines = datafile.readlines()
for line in all_lines:
if line:
# rest of your logic