Python - Combing data from different .csv files. into one -


i need python programmers solve issue i'm facing in processing data:-

  • i have .csv files placed in directory structure this:-

    -maindirectory

    • sub directory 1
      • sub directory 1a
        • fil.csv
    • sub directory 2
      • sub directory 2a
        • file.csv
    • sub directory 3
      • sub directory 3a
        • file.csv

    instead of going each directory , accessing .csv files, want run script can combine data of sub directories.

each file has same type of header. , need maintain 1 big .csv file 1 header , .csv file data can appended 1 after other.

i have python script can combine files in single file when files placed in 1 folder.

can provide script can handle above directory structure?

try code, tested on laptop,it works well!

import sys import os  def mergecsv(srcdir,destcsv):     open(destcsv,'w') destfile:         header=''         root,dirs,files in os.walk(srcdir):             f in files:                 if f.endswith(".csv"):                     open(os.path.join(root,f),'r') csvfile:                         if header=='':                             header=csvfile.readline()                             destfile.write(header)                         else:                             csvfile.readline()                         line in csvfile:                             destfile.write(line)            if __name__ == '__main__':     mergecsv('d:/csv','d:/csv/merged.csv') 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -