Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于从日志文件提取测试的Python脚本_Python - Fatal编程技术网

用于从日志文件提取测试的Python脚本

用于从日志文件提取测试的Python脚本,python,Python,问题陈述如下,有一个日志文件包含与测试结果相关的日志。例如,它包含文本,如testcase1后面跟测试用例的日志,testcase2后面跟测试用例的日志等等 若用户想要提取testcase1和testcase3的日志,那个么脚本应该读取来自用户的输入,比如testcase1和testcase3。然后仅提取指定测试用例的日志 在这种情况下,假设用户输入testcase1和testcase3,输出应该是日志文件中testcase1和testcase3下面的行。这里您必须假设所有测试用例日志都在单独的

问题陈述如下,有一个日志文件包含与测试结果相关的日志。例如,它包含文本,如testcase1后面跟测试用例的日志,testcase2后面跟测试用例的日志等等

若用户想要提取testcase1和testcase3的日志,那个么脚本应该读取来自用户的输入,比如testcase1和testcase3。然后仅提取指定测试用例的日志


在这种情况下,假设用户输入testcase1和testcase3,输出应该是日志文件中testcase1和testcase3下面的行。

这里您必须假设所有测试用例日志都在单独的行中,这意味着您在每个日志行后面都有“\n”

然后可以从linecache模块读取文件。 现在,您应该以特定的格式创建日志。这里您提到它是[testcaseN][Log message],并且[testcaseN]应该有'testcase'和'N'作为变量


因此,当您使用linecache模块获取所有行时,请使用re模块将作为输入的testcaseN与获取的单个行的第一个字进行匹配。获得匹配后,显示结果。

最终获得了提取文本的工作脚本

#从文本文件中读取特定行 #例如,在程序中,我们读取文件并仅打印标题2和标题4下的行 #日志文件可能包含作为weel的空行 #示例日志文件 #头衔 # 1 #dklfjsdkl; #g #sdfzsdfsdf #sdfsdfsdf #DSFSD #dfsdf # #头衔 # 2 # #dfdf #dfdf #dfdf #df #dfd #d # #标题3 #sdfdfd #DFD #dfd # #dfd # #头衔 # 4 #dfkdfkd #dfdkjmd #dfdkljm

in_list= []
while True:
    i = raw_input("Enter title to be extracted (or Enter to quit): ")
    in_list.append(i)
    if not i:
        break

    print("Your input:", i)
print("While loop has exited")
in_list.remove(i)
print "Input list", in_list

flist = []
with open("C:\\text.txt", 'r') as inp:
    #read the flie and storing into the list
    flist =inp.readlines()
    inp.close()

#making everything in the list to lower case
flist = map(lambda x:x.lower(),flist)
flist = [s.strip("\n") for s in flist]
print flist

    # printing the complete log file from the list. Since once we put the vlaue in the list the new line character will be \ appended in the list element.
    #hence striping with \n character
# for i in flist:
#     print i.strip("\\n")

for j in range(len(in_list)):
    result = any(in_list[j] in word for word in flist)
    if result:
        i_index = flist.index(in_list[j])
        flag = 0
        with open("C:\\output.txt",'a') as f1:
            f1.write(flist[i_index])
            f1.write("\n")

            while flag ==0:
                if "title" in flist[i_index+1]:
                    flag =1
                else:
                    i_index += 1
                    f1.write(flist[i_index])
                    f1.write("\n")
                    i_index += 1

f1.close()

你能展示一些示例日志吗?您的问题陈述过于笼统,无法让其他人提供帮助。请发布您实现的代码,我们不会为您编写代码。如果您的代码有任何问题,请在此处发布错误。