0

我现在有四个函数来分析 csv 文件。第一个格式化文档并输出(返回)四个值。第二个和第三个从第一个函数中获取四个值并计算(并返回)一些其他值。主函数(第四个)需要从第二个和第三个函数返回值。

我的问题是主函数需要有一个参数(文档名称),我需要将其传递给第一个函数(显然)以便对其进行格式化,但我不知道该怎么做。出于测试目的,我不能使用打印或输入功能,那我该怎么做呢?

我的代码看起来有点像这样:

def formatting():
   with open('marks.csv', 'r') as f:
   ...
   n = 30
   m = 9
   array = [...]
   tot = [...]
   return m, n, array, tot

def calculation():
   n, m, array, tot = formatting()
   ...
   mn = [...]
   mx = [...]
   av = [...]
   sd = [...]
   return mn, mx, av, sd

def cor():
   n, m, array, tot = formatting()
   ...
   rs = [...]
   return rs

def main():
   mn, mx, av, sd = calculation()
   rs = cor()
   return(mn, mx, av, sd, rs)
main()

但是我需要在 main 中有一个参数,它像这样传递给格式化函数:

main(csvfile)

传递给格式化,以便我有

with open(csvfile, 'r') as f:

反而。

谢谢!

4

1 回答 1

0
def formatting(csvfile):
   with open(csvfile, 'r') as f:
   ...
   n = 30
   m = 9
   array = [...]
   tot = [...]
   return m, n, array, tot

def calculation(csvfile):
   n, m, array, tot = formatting(csvfile)
   ...
   mn = [...]
   mx = [...]
   av = [...]
   sd = [...]
   return mn, mx, av, sd

def cor(csvfile):
   n, m, array, tot = formatting(csvfile)
   ...
   rs = [...]
   return rs

def main(csvfile):
   mn, mx, av, sd = calculation(csvfile)
   rs = cor(csvfile)
   return(mn, mx, av, sd, rs)

main('marks.csv')
于 2020-05-02T07:48:48.870 回答