【问题标题】:Extracting multiple Excel files from different paths in python at once while they are zipped in various places一次从python中的不同路径提取多个Excel文件,同时将它们压缩到不同的地方
【发布时间】:2021-12-20 23:46:43
【问题描述】:

我有 30 个文件夹(从 2021-06-012021-06-30),每个文件夹中有 15 个 excel 文件。目前我分别使用此代码 30 次

file1= glob.glob('C:/Users/Dell/Downloads/2021-06-01/*')

制作一个文件file1,为每个文件夹运行数据处理操作(进入15个excel文件)。这样我就可以有file 1file 30然后我concat他们制作一个文件。 有什么方法可以自动化这个过程,因为我不想单独运行这个操作 30 次?我没有弄清楚如何为此创建一个循环来从不同的路径中提取文件。 我也有数据,但它们被压缩在文件夹中(从 2021-06-012021-06-30)。所以,一个个去那里解压然后一个个运行操作是很繁琐的。 我怎样才能以更简单的方式实现这两个目的?我通过搜索看到了解压操作解决方案,我无法做到,同时我还必须获得我提到的另一个目的(遍历不同的文件夹并一次一个地提取它们,使file 1file 30 一次) 我的目录看起来像:

- download                                                                                                                           
       -month                                                                                                                                
                 -2021-01-01 
                     -AA                                                                                                         
                          -file.zip                                                                                                                                       
                              -a list of .xlsx file 
                     -BB
                     -CC                                                                                  
                 -2021-01-02 
                     -AA                                                                                                          
                          -file.zip                                                                                                                                     
                               -a list of .xlsx file                                                                                            
                     -BB
                     -CC
 ........................................................................................................................................................................... 
                 -2021-01-30        

现在我不想连接这些 xlsx 文件。我想对这些excel文件一个一个地运行某个操作,然后将它们连接起来。但是不能这样做。

【问题讨论】:

    标签: python excel pandas dataframe data-manipulation


    【解决方案1】:

    这是一个适合您的 Python 脚本:

    import os
    import shutil
    import time
    
    import pandas as pd
    
    
    def read_csv_or_excel(f):
        if f.endswith(".csv"):
            df = pd.read_csv(f"{root}/{f}", sep="\t")
        if f.endswith(".xlsx"):
            df = pd.read_excel(f"{root}/{f}")
        return df
    
    
    for root, dirs, files in os.walk("./questions/69878352/"):
        #     print(root, dirs, files)
        if root.split("/")[-1].startswith("20"):
            print(root)
            appended = []
            dfs = []
            for f in files:
                if f not in appended:
                    print(f)
                    if f.endswith(".csv") or f.endswith(".xlsx"):
                        dfs.append(read_csv_or_excel(f))
                    elif f.endswith(".zip"):
                        shutil.unpack_archive(f"{root}/{f}", f"{root}/")
                        time.sleep(0.5)
                        f = f"{f[:-4]}.xlsx" # ← this assumes any zipped files will be Excel files...
                        dfs.append(read_csv_or_excel(f))
                    else:
                        continue
                    appended.append(f[:-4])
            pd.concat(dfs).to_excel(f"{root}.xlsx"
    

    Lmk 如果它不起作用!我的测试数据不是最好的,我必须花更多时间制作更好的测试数据才能 100% 起作用,所以如果你有任何问题,可能只是需要修改它的小调整?

    【讨论】:

    • 不幸的是它不起作用。我编辑了我的问题,显示了我的目录是如何排列的。我希望这有助于更好地理解场景
    • 那么你明白我在那里用代码做什么了吗?它也许可以作为您修改的良好起点。如果没有任何特定的回溯错误,很难进一步提供帮助。看起来您需要稍微修改我提供的代码,如果您要使用它,以说明以日期命名的目录下的子目录(即“2021-....”等) .那只是一个小调整
    • 在您的情况下,我可能更喜欢使用bash 而不是python。 Lemme 提供了另一个简短的答案,快速展示了它是如何工作的,如果这可能会有所帮助
    【解决方案2】:

    您也可以尝试在终端中仅使用 bash:

    $ find . -maxdepth 5 -name *.zip | parallel unzip # this will unzip everything in one command
    $ find . -maxdepth 5 -name *.xlsx | parallel # perform whatever operation you want on all the excel files
    

    【讨论】:

    • 另一个简单的无代码选项是使用 Automator,如果你碰巧在 Mac 上。所有 Mac 都已安装 Automator;使用方法非常直观
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多