【发布时间】:2017-04-19 13:59:02
【问题描述】:
def production(csvfile):
# do something with csvfile
print csvfile
production("somecsvfile.csv")
我想传递 3 个 csv 文件,程序应该给出它的输出。目前我已经给出了一个 csv 文件。
【问题讨论】:
def production(csvfile):
# do something with csvfile
print csvfile
production("somecsvfile.csv")
我想传递 3 个 csv 文件,程序应该给出它的输出。目前我已经给出了一个 csv 文件。
【问题讨论】:
使用 python *args 参数解包,可以接受任意数量的参数。它适用于任意数量的参数
def production(*csvfile):
for csv in csvfile:
# do something with csvfile
print(csv)
production("file1.csv", "file2.csv", "file3.csv")
# code above will printout
# file1.csv
# file2.csv
# file3.csv
production("file1.csv")
# just print out
# file1.csv
如果您希望它是精确的 3 个输入文件,您可以定义如下所示的 3 个输入或 1 个也适用于任何列表长度的列表输入
def production(csvfile1, csvfile2, csvfile3):
for csv in [csvfile1, csvfile2, csvfile3]:
# do something with csvfile
print(csv)
【讨论】:
在函数中添加以下内容
fileList = csvfiles.split(",")
for files in filesList:
data = read_csv(csvfile)
....
....
production("rswm20160901C.csv,rswm20160901E.csv,rswm20160901D.csv")
或
import sys
def production(csvfile)
......
......
fileList = sys.argv[1:] # pass the functions in command line
for f in fileList:
production(f)
【讨论】:
我们不需要使情况过于复杂。 应该这样做:
def production(csvfile)
......
......
fileList = ['a.csv','b.csv','c.csv']
for f in fileList:
production(f)
【讨论】: