简体   繁体   English

如何将 excel/csv 文件作为 function 参数传递?

[英]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.

相关问题 如何将对象属性作为函数参数传递给lambda函数? - How can I pass an object attribute to lambda function, as function parameter? 如何使用 openpyxl 将 csv 文件写入 Excel 工作表? - How can i write a csv file to an excel sheet using openpyxl? 如何将 output 从 function 写入 csv 文件? - How can I write the output from a function to a csv file? 如何在 Python 中的 CSV 文件中为空单元格编写 function? - How can I write a function for empty cell in CSV file in Python? 我可以在一个 Excel 文件或 CSV 文件中为相同的字符串着色吗? - Can I color the same string in one Excel file or CSV file? 我如何打印 pandas dataframe 的属性/元数据,同时将其保存在 Excel 或 ZCC8D6DZD313 文件中? - How can I print attributes/meta-data of a pandas dataframe while saving it in Excel or CSV file? 为什么我不能将 function 作为参数传递给随机数? - why can't I pass the function as a parameter to the random? 我可以在python3中从Excel文件(不是CSV)创建字典吗? - Can I create a dictionary from excel file (not CSV) in python3? 如何读取 csv 文件并将其值传递给 function 参数 - how to read csv file and pass its values to a function parameters 如何将变量值作为参数传递给格式 function - How do I pass variable value as parameter to format function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM