从文件读取并调用函数

2024-10-03 00:16:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个任务,我需要从文本文件中读取并调用一个函数。文本文件如下:

black,20,10,3,1
red,10,20,4,3
blue,10,-20,-4,3

我定义的函数有五个参数,在文本flile中用逗号分隔。 到目前为止,我的情况是:

with open(textfile) as source:
    for i in source.readlines():
        a = split(",")

但是在这里我不知道如何用源代码的read行调用函数。你知道吗

有什么想法吗?你知道吗


Tags: 函数文本source参数定义with情况blue
2条回答

如果您的函数是f,那么只需调用f(*a)。你知道吗

我最终得到了这个,它正在发挥作用。你知道吗

def piirra_tiedostosta(tiedosto):
    with open(tiedosto) as source:
        for i in source.read().splitlines():
        c, a, r, n, w = i.split(",")
        a = int(a)
        r = int(r)
        n = float(n)
        w = int(w.strip())

现在我把所有变量分开,我可以调用这个函数。你知道吗

f(c、a、r、n、w)

相关问题 更多 >