Python - 从不同的.csv文件合并数据。合二为一

问题描述:

我需要一些来自python程序员的帮助来解决我在处理数据时遇到的问题: -

I need some help from python programmers to solve the issue I'm facing in processing data:-


  • .csv文件放置在目录结构中,如下所示: -

  • I have .csv files placed in a directory structure like this:-

-MainDirectory

-MainDirectory


  • 子目录1

    • 子目录1A

      • fil.csv


      • 子目录2A

        • file.csv


        • 子目录3A

          • file.csv

          我想运行一个脚本,而不是进入每个目录并访问.csv文件组合所有子目录的数据。

          Instead of going into each directory and accessing the .csv files, I want to run a script that can combine the data of the all the sub directories.

          每个文件的标题类型相同。我需要维护一个大的.csv文件,只有一个头,所有的.csv文件数据可以一个接一个附加。

          Each file has the same type of header. And I need to maintain 1 big .csv file with one header only and all the .csv file data can be appended one after the other.

          我有一个python脚本,可以组合单个文件中的所有文件,但只有当这些文件放在一个文件夹。

          I have the python script that can combine all the files in a single file but only when those files are placed in one folder.

          您能帮助提供一个可以处理上述目录结构的脚本吗?

          Can you help to provide a script that can handle the above directory structure?

尝试这个代码,我在我的笔记本电脑上测试它,它工作得很好!

Try this code, I tested it on my laptop,it works well!

import sys
import os

def mergeCSV(srcDir,destCSV):
    with open(destCSV,'w') as destFile:
        header=''
        for root,dirs,files in os.walk(srcDir):
            for f in files:
                if f.endswith(".csv"):
                    with open(os.path.join(root,f),'r') as csvfile:
                        if header=='':
                            header=csvfile.readline()
                            destFile.write(header)
                        else:
                            csvfile.readline()
                        for line in csvfile:
                            destFile.write(line)          

if __name__ == '__main__':
    mergeCSV('D:/csv','D:/csv/merged.csv')