[英]How can I pass a excel/csv file as a function parameter?
How can I pass a excel/csv file as a function parameter?如何将 excel/csv 文件作为 function 参数传递? I have wrote a piece of code to copy content from one excel file to another excel file.
我编写了一段代码,将内容从一个 excel 文件复制到另一个 excel 文件。 Now I want to define it as a function so I just have to mention file name from which I want to transfer data to my existing file.
现在我想将它定义为 function 所以我只需要提及我想将数据传输到现有文件的文件名。 Thanks a lot in advance.
提前非常感谢。 I am very new to Python Programming, and looking forward to learn a lot.
我对 Python 编程非常陌生,期待学到很多东西。
def feed_input_file(InputFile): def feed_input_file(InputFile):
InputFile = "D:\\Python_Projects\\ABC.xlsx" #(I am passing Input file at the moment but I don't wanna pass it here)
#( Here I am trying to call my function parameter value) #(这里我试图调用我的 function 参数值)
Workbook1 = xl.load_workbook(feed_input_file(InputFile)) Workbook1 = xl.load_workbook(feed_input_file(InputFile))
............ …………
Still not quite sure what you are trying to do but if you want to create a function that will take a filename as an argument you could try something along the lines of:仍然不太确定您要做什么,但是如果您想创建一个 function ,它将以文件名作为参数,您可以尝试以下方式:
def processFile(fn:str):
#Reads in the contents of file specified by fn
content = ''
with open(fn, 'a') as f:
data = f.read()
#Do something with data here to create content
return content
Then in your main part of the script然后在脚本的主要部分
for filename in listofFiles:
fle_out = processFile(filename
#Do something here with file contents
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.