File tree Expand file tree Collapse file tree 1 file changed +24
-20
lines changed
Expand file tree Collapse file tree 1 file changed +24
-20
lines changed Original file line number Diff line number Diff line change 1- import csv
2- import glob
1+ 2+
3+ #!/usr/bin/env python
4+ # -*- coding=utf-8 -*-
5+
36import os
4- import pdb
5- import pandas as pd
67
8+ # define the result filename
9+ resultfile = 'result.csv'
10+
11+ # the merge func
12+ def merge ():
13+ """merge csv files to one file"""
14+ # use list save the csv files
15+ csvfiles = [f for f in os .listdir ('.' ) if f != resultfile and f .split ('.' )[1 ]== 'csv' ]
16+ # open file to write
17+ with open (resultfile ,'w' ) as writefile :
18+ for csvfile in csvfiles :
19+ with open (csvfile ) as readfile :
20+ print ('File{} readed.' .format (csvfile ))
21+ # do the read and write
22+ writefile .write (readfile .read ()+ '\n ' )
23+ print ('\n File{} writed.' .format (resultfile ))
724
8- def main ():
9- directory = []
10- for dirs in os .walk ("." ):
11- directory .append (dirs )
12- folders = directory [0 ][1 ]
13- for ff in folders :
14- if ff != ".git" :
15- allFiles = glob .glob (ff + "/*.csv" )
16- frame = pd .DataFrame ()
17- dfs = []
18- for files in allFiles :
19- df = pd .read_csv (files , index_col = None , header = 0 )
20- dfs .append (df )
21- frame = pd .concat (dfs )
22- frame .to_csv (ff + "/results.csv" )
23- main ()
25+ # the main program
26+ if __name__ == '__main__' :
27+ merge ()
You can’t perform that action at this time.
0 commit comments