如何为文件夹中包含的每个Excel文件运行我的Python代码?

发布于 2025-01-23 07:38:43 字数 817 浏览 4 评论 0原文

我有一个名为“特定首字母缩写”的文件夹,在此文件夹中,您可以找到一定数量的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹短裙飘 2025-01-30 07:38:43

在以下代码中,只能跟踪您的路径,在我的情况下,我使用了一个测试文件夹路径path = r'd:\ test'此代码将在同一路径中自动创建一个新文件夹。

import pandas as pd
import os
from glob import glob

path=r'D:\test'  # add whatever your path is in place of 'D:\test'
input_folder='UDC06_45' # name of input folder
output_folder=input_folder+'_out' 

new_path=path+'/'+output_folder 


if not os.path.exists(new_path):
    os.makedirs(new_path)
    
files=glob(path+'/'+input_folder+'/'+'*.xlsx')

for file in files:
    name=file.split(path+'/'+input_folder+'\\')[-1].rsplit('.')[0]
    df=pd.read_excel(file)
    #do all your operations here
    df.to_excel(new_path+'/'+name+'_output.xlsx')

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.

import pandas as pd
import os
from glob import glob

path=r'D:\test'  # add whatever your path is in place of 'D:\test'
input_folder='UDC06_45' # name of input folder
output_folder=input_folder+'_out' 

new_path=path+'/'+output_folder 


if not os.path.exists(new_path):
    os.makedirs(new_path)
    
files=glob(path+'/'+input_folder+'/'+'*.xlsx')

for file in files:
    name=file.split(path+'/'+input_folder+'\\')[-1].rsplit('.')[0]
    df=pd.read_excel(file)
    #do all your operations here
    df.to_excel(new_path+'/'+name+'_output.xlsx')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文