Py学习  »  Python

Python:无法在第一个函数的结果之后移动到第二个函数

Christopher Pisano • 5 年前 • 1485 次点击  

项目目标: 搜索定义的yamlfile( scan_dcn.yaml function_search_search_key() function_search_event_type() 功能。

输入文件-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
        },

我的程序将返回 函数搜索键() 但不会继续

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

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()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53239
 
1485 次点击  
文章 [ 3 ]  |  最新文章 5 年前
azro
Reply   •   1 楼
azro    5 年前

你只能读取一次文件描述符(如果你不想启动的话),所以你可以在每个函数中打开你的文件

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)
Carcigenicate
Reply   •   2 楼
Carcigenicate    5 年前

您的第二个功能正在进入。如果上面的通话结束了,那一定是。

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)
Matthias
Reply   •   3 楼
Matthias    5 年前

在第一个函数中,用 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()

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