如何为文件夹中包含的每个Excel文件运行我的Python代码?
我有一个名为“特定首字母缩写”的文件夹,在此文件夹中,您可以找到一定数量的Excel文件。 该文件夹的名称指示公寓的名称(对于Ex。UDC06_45),在此文件夹中,所有Excel文件的名称均由: 公寓的名称,然后是该公寓中设备的名称(对于UDC06_45_OVEN)。
这些Excel文件是非常简单的数据框,它们包含能耗测量值:一列名为“ Timestamps”和一个名为“ Energy”的列(所有这些测量值的频率为15分钟)。文件夹中的所有Excel文件均使用相同的相同结构制成。
我的python代码一次以输入为一次一次一次以“ pd._excel()”和给它一个名称后,使用“ df.to_excel()”创建输出Excel文件。
我要做的是将我的代码自动应用于该文件夹中的所有文件。 该代码应仅作为输入,仅将文件夹的名称(“ UDC06_45”)作为输入,并根据需要创建尽可能多的输出文件。 因此,如果文件夹仅包含两个设备:
- “ udc06_45_oven”
- ““ udc06_45_fridge”,
代码将它们均详细介绍,一个接一个,我应该将两个Din -stinct Excel文件作为输出。他们的名称仅由输入文件的名称组成,然后是“ _Output”:
- “ udc06_45_oven_output”
- “ udc06_45_fridge_output”。
通常,必须对该文件夹中包含的每个Excel文件完成此操作。如果该文件夹包含5个设备,则为5个输入Excel文件,我应该获得5个输出Excel文件...等等。
我该怎么做?
I have a folder named with a certain acronym, and inside this folder you can find a certain number of Excel files.
The folder's name indicates the name of the apartment (for ex. UDC06_45) and, inside this folder, all of the Excel files' name are composed by:
the name of the apartment, followed by the name of the appliance that is located in that apartment (for ex. UDC06_45_Oven).
These Excel files are very simple DataFrames, they contain energy consumption measurements: one column named "timestamps" and one column named "Energy" (all of these measurements have a 15 min frequency). All of the Excel files inside the folder are made with the same identical structure.
My Python code takes as input only one of these Excel files at a time and makes few operations on them (resampling, time interpolation, etc.) starting with the command "pd.read_excel()", and creates an output Excel file with "df.to_excel()" after giving it a name.
What I want to do is to apply my code automatically to all of the files in that folder.
The code should take as input only the name of the folder ("UDC06_45") and create as many output files as needed.
So if the folder contains only two appliances:
- "UDC06_45_Oven"
- "UDC06_45_Fridge"
the code will elaborate them both, one after the other, and I should obtain two dinstinct Excel files as output. Their name is just composed by the input file's name followed by "_output":
- "UDC06_45_Oven_output"
- "UDC06_45_Fridge_output".
In general, this must be done for every Excel file contained in that folder. If the folder contains 5 appliances, meaning 5 input Excel files, I should obtain 5 output Excel files... and so on.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在以下代码中,只能跟踪您的路径,在我的情况下,我使用了一个测试文件夹路径
path = r'd:\ test
'此代码将在同一路径中自动创建一个新文件夹。In the following code only assing your path, in my case I have used a test folder path
path=r'D:\test
' this code will create a new folder automatically in the same path.