在第一个功能执行完后无法移至第二个功能

时间:2020-01-19 16:24:05

标签: python

程序目标:搜索定义的yaml文件(scan_dcn.yaml),并返回与function_search_search_key()function_search_event_type()函数中定义的搜索条件相匹配的所有行。 / p>

输入文件-scan_dcn.yaml:

search_dict:
    [
        {search_key: ["Failed to Process the file"],
        event_type: "evttyp_repl_dcn_error",
        event_description: "Failure to process DCN file",
        priority: 50,
        scan_interval: 1,
        remove_dups: True,
        category: "dcn",
        context_begin: 0,
        context_end: 1,
        reportable: False,
        offset: 0
        },

问题: 我的程序将返回function_search_search_key(),但不会继续进行function_search_event_type()

我认为我的问题是,在第一个功能完成之后,我没有逻辑去进行第二个功能。

是否需要在每个函数中返回一个值才能继续?

Python源代码

yamlfile = open('scan_dcn.yaml', 'r')


def function_search_search_key():
    search_search_key = ['{search_key:']
    for line in yamlfile.readlines():
        for word in search_search_key:
            if word in line:
                print(line)


def function_search_event_type():
    search_event_type = ['event_type:']
    for line in yamlfile.readlines():
        for word in search_event_type:
            if word in line:
                print(line)


def main():
    function_search_search_key()
    function_search_event_type()


main()

3 个答案:

答案 0 :(得分:0)

正在输入您的第二个功能。必须完成上面的调用。

您没有看到任何打印内容,因为您试图多次遍历同一文件。读取文件后,它就筋疲力尽了。您可以重新读取文件作为简单的解决方法:

def function_search_search_key():
    with open('scan_dcn.yaml', 'r') as yamlfile:
        search_search_key = ['{search_key:']
        for line in yamlfile.readlines():
            for word in search_search_key:
                if word in line:
                    print(line)


def function_search_event_type():
    with open('scan_dcn.yaml', 'r') as yamlfile:  # Read the file again
        search_event_type = ['event_type:']
        for line in yamlfile.readlines():
            for word in search_event_type:
                if word in line:
                    print(line)

答案 1 :(得分:0)

在您的第一个函数中,您使用readlines读取了整个文件。当您在第二个功能中再次使用readlines时,您已经在文件末尾,没有更多数据可读取,因此甚至没有输入for循环。

但是没有必要为每个功能再次读取文件。读取功能之外的文件并将其放在列表中。然后向每个采用此列表的函数添加一个参数。在该功能中,您可以遍历列表。

def function_search_search_key(lines):
    search_search_key = ['{search_key:']
    for line in lines:
        for word in search_search_key:
            if word in line:
                print(line)


def function_search_event_type(lines):
    search_event_type = ['event_type:']
    for line in lines:
        for word in search_event_type:
            if word in line:
                print(line)


def main():
    with open('scan_dcn.yaml', 'r') as yamlfile:
        lines = yamlfile.readlines()
    function_search_search_key(lines)
    function_search_event_type(lines)

if __name__ = '__main__':
    main()

如果您需要更改文件名,可以在一个地方进行。如果您在每个函数中打开并读取文件,则必须更改文件名的所有出现位置。

答案 2 :(得分:0)

您只能读取一次文件描述符(如果不尝试启动),因此可以在每个函数中打开文件

def function_search_search_key():
    search_search_key = ['{search_key:']
    with open('scan_dcn.yaml') as fic:
        for line in fic:
            for word in search_search_key:
                if word in line:
                    print(line)

def function_search_event_type():
    search_event_type = ['event_type:']
    with open('scan_dcn.yaml') as fic:
        for line in fic:
            for word in search_event_type:
                if word in line:
                    print(line)