Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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_Search_Text - Fatal编程技术网

使用Python从第二个文件的列表中搜索一个文件

使用Python从第二个文件的列表中搜索一个文件,python,search,text,Python,Search,Text,这是我第一次在这里发帖,我只是在学习python,所以请容忍我。 我已经在这里和网上的其他地方仔细研究了一下,试图找到一个答案,但是,唉,网络似乎被剥夺了 我有一个每天生成的日志文件,它可以包含许多不同语法的错误。其中一些是重要的,而另一些则可以忽略不计,但由于它们都包含文本“Error”,我不能只搜索一个单词 我有一个以前在一个文件中看到的错误文本列表,我想将日志文件与它进行比较,以查找匹配项,以便在看到任何错误时发出警告 我已经设法让它为一个错误工作,但我不能让它为列表工作 工作代码如下所示

这是我第一次在这里发帖,我只是在学习python,所以请容忍我。 我已经在这里和网上的其他地方仔细研究了一下,试图找到一个答案,但是,唉,网络似乎被剥夺了

我有一个每天生成的日志文件,它可以包含许多不同语法的错误。其中一些是重要的,而另一些则可以忽略不计,但由于它们都包含文本“Error”,我不能只搜索一个单词

我有一个以前在一个文件中看到的错误文本列表,我想将日志文件与它进行比较,以查找匹配项,以便在看到任何错误时发出警告

我已经设法让它为一个错误工作,但我不能让它为列表工作

工作代码如下所示:

    log_file = open ('a file location' , 'r')
    read_file = file.read ()
    search = read_file.find ('Error type 1 happened at x')
    if search != -1:
        print 'Error found in  log at point ' + str((search)) + '!!!'
    else :
        print "All is good!!!  :-D "
对于列表,我尝试了以下方法:

    load_log_file = open ('a file location' , 'r')
    read_log_file = load_log_file.read ()

    def txt_search (read_log_file):
        errors = open ('another file location' , 'r')
    for error in errors.readlines() :
            if error.strip ('\r\n') in read_log_file:
                return 'Error found in log !!!'
            else :
                return "All is good!!!  :-D "

    print txt_search (read_log_file)
我在运行时得到了一个结果,但它总是以“一切都很好”返回。 我可能错过了一些非常明显的东西,但任何帮助都会得到很大的回报


非常感谢你过早地回来了

load_log_file = open ('a file location' , 'r')
read_log_file = load_log_file.read ()

def txt_search (read_log_file):
    errors = open ('another file location' , 'r')
    for error in errors.readlines() :
        if error.strip ('\r\n') in read_log_file:
            return 'Error found in log !!!'

    return "All is good!!!  :-D "

print txt_search (read_log_file)

原始代码中发生的情况是,它查找第一个错误,但没有找到,然后返回“一切正常”。请始终记住,函数结束时使用
return
语句。

如果您还可以发布一个示例日志文件(几行),并且在继续获得一些结果的同时开始进行简短的示例扩展,那将是最好的。好的开始是在
rU
模式下打开文件(
U
代表通用的行结束),当你使用
'line'进行剥离时。剥离(os.linesep)
@BobHoward:别担心,我们都会时不时地搞砸。威瑟尔:您不知道
return
(在这种情况下,请学习!)。或者你忘记了,你只是忘记了(在我身上发生过无数次)。我只是说:)