如何在Python中解析索引不断变化的动态行的txt文件
我是Python新手。 需要使用动态变量(更改索引)解析文本文件 我有下面的文本文件如何在Python中解析索引不断变化的动态行的txt文件,python,parsing,Python,Parsing,我是Python新手。 需要使用动态变量(更改索引)解析文本文件 我有下面的文本文件 packet1_ref_time = 13.64 packet2_ref_time = 73.68 16.11.2018 16:33:03 {Step 1 passed successfully!} 16.11.2018 16:33:06 {} packet1_ref_time = 17.25 packet2_ref_time = 71.112 16.11.2018 16:33:03 {Step 2 Faile
packet1_ref_time = 13.64
packet2_ref_time = 73.68
16.11.2018 16:33:03 {Step 1 passed successfully!}
16.11.2018 16:33:06 {}
packet1_ref_time = 17.25
packet2_ref_time = 71.112
16.11.2018 16:33:03 {Step 2 Failed!}
16.11.2018 16:33:06 {}
packet1_ref_time = 13.877
packet2_ref_time = 78.366
16.11.2018 16:33:03 {Step 3 passed successfully!}
16.11.2018 16:33:06 {}
我的最终输出\另一个文本文件应为:
测试1通过
测试2失败
测试3通过
def main():
file = open("Console_log_28-12-2018_02-31-55.txt","r")
lines = file.readlines()
file.close()
for line in lines:
line=line.strip()
index = 1
str (index)
#if line == "Step"index"passed successfully!":
if line.find("Step 1 passed successfully!") != -1:
print ( line )
main()
结果是“28.12.2018 02:36:16{步骤1成功通过!}”
正如所料。
这很明显,因为我正在寻找一个确切的步骤(本例中的步骤1)
我需要它来搜索动态索引,就像我在评论中写的那样:
#如果行==“步骤”索引“成功通过!”:
但是我可以;我找不到正确的语法来编写代码 对于查找行,可以使用正则表达式,即
re
模块
for line in lines:
if bool(re.search('Step [0-9]+ (passed successfully)|(Failed)!',line)):
print(line)
[0-9]+
表示任何自然数,而|
表示或在re
中,在本例中成功通过或失败
编辑:在重新思考了你的问题后,我得出结论,findall会更好,请参见下面的示例
text = '''better in this case
packet1_ref_time = 13.64
packet2_ref_time = 73.68
16.11.2018 16:33:03 {Step 1 passed successfully!}
16.11.2018 16:33:06 {}
packet1_ref_time = 17.25
packet2_ref_time = 71.112
16.11.2018 16:33:03 {Step 2 Failed!}
16.11.2018 16:33:06 {}
packet1_ref_time = 13.877
packet2_ref_time = 78.366
16.11.2018 16:33:03 {Step 3 passed successfully!}
16.11.2018 16:33:06 {}'''
tests = re.findall('Step [0-9]+ passed|Step [0-9]+ Failed',text)
print(tests)
输出:
['Step 1 passed', 'Step 2 Failed', 'Step 3 passed']
请注意,为了清楚起见,我只是将字符串分配给文本
。您应该将.read()
返回的值赋给变量
outputlist = []
with open("Console_log_28-12-2018_02-31-55.txt","r") as file:
lines = file.readlines()
for line in lines:
if 'passed successfully' in line:
loc = line.find('passed')
test, num = 'pass', line[:loc].split()[-1]
outputlist.append((num, test))
elif 'Failed' in line:
loc = line.find('Failed')
test, num = 'fail', line[:loc].split()[-1]
outputlist.append((num, test))
print (outputlist)
#[('1', 'pass'), ('2', 'fail'), ('3', 'pass')]
说明:
test,num='pass',line[:loc].split()[-1]
将为您获取检测到满足条件的行中的最后一个数字。这有助于您考虑尽可能多的测试用例,只要它们被
拆分
outputlist
是一个元组列表,它将告诉您哪些测试用例通过或失败
因此,您可以在outputlist中执行for循环,以打印出所需的语句
for i, v in outputlist:
print (f'Test {i} {v}ed')
#Test 1 passed
#Test 2 failed
#Test 3 passed
另外,使用和
打开文件,这样您就不用调用f.close()
<使用
的code>将调用dunder方法\uuuu exit\uuuu
来帮助关闭文件您可以使用字符串格式运算符和字符串参数来查找函数,如下所述。字符串格式运算符可以在需要替换字符串的任何位置使用
def main():
file = open("Console_log_28-12-2018_02-31-55.txt","r")
lines = file.readlines()
file.close()
for line in lines:
line=line.strip()
index = 1
str (index)
#if line == "Step"index"passed successfully!":
if line.find("Step %d passed successfully!"%index) != -1:
print ( line )
main()
要了解更多信息,请参阅文档日志文件是否每个步骤都有固定的4行?如果是这样的话,你可以每四行打印一次。