简体   繁体   English

在Python中使用变量名导出excel文件

[英]Export an excel file using a variable name in Python

I am new to python, trying to figure out dynamic naming of exported files.我是 python 新手,试图找出导出文件的动态命名。 Right now, I am exporting an xlsx file in a traditional way:现在,我正在以传统方式导出 xlsx 文件:

data_subset.to_csv('Destination/existing_process.csv') data_subset.to_csv('Destination/existing_process.csv')

I have a string called 'user_name' and the resultant tuple for every user is exported into an excel file called existing_process.我有一个名为“user_name”的字符串,每个用户的结果元组都导出到一个名为 existing_process 的 Excel 文件中。 Instead of the name existing_process, I would like to rename the file dynamically using the string user_name.我想使用字符串 user_name 动态重命名文件,而不是名称 existing_process。

For instance, for a given user_name 'Matt' I would like the exported file to be named as Matt.csv.例如,对于给定的 user_name 'Matt',我希望将导出的文件命名为 Matt.csv。 Thanks!谢谢!

You can use string concatenation for that.您可以为此使用string concatenation

name = 'Matt'
with open(name + '.txt', 'w') as f:
    f.write('test')

The same you can use with xlsx but you will you using it slightly different, but the concatenation process is same.您可以与xlsx使用,但您会使用它略有不同,但连接过程是相同的。 Kindly let me know if you want the exact procedure with xlsx如果您想要xlsx的确切程序,请告诉我

Here is the solution:这是解决方案:

inv.name = 'Rem_inv'

dataframe_list = [inv]
for dataframe in dataframe_list:
    dataframe.to_csv(df.name + '.csv') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM