Skip to content

Commit f0b96cb

Browse files
authored
Update merge.py
modify the program more simply
1 parent fa6d44d commit f0b96cb

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

‎merge.py‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
importcsv
2-
importglob
1+
2+
3+
#!/usr/bin/env python
4+
# -*- coding=utf-8 -*-
5+
36
importos
4-
importpdb
5-
importpandasaspd
67

8+
# define the result filename
9+
resultfile='result.csv'
10+
11+
# the merge func
12+
defmerge():
13+
"""merge csv files to one file"""
14+
# use list save the csv files
15+
csvfiles= [fforfinos.listdir('.') iff!=resultfileandf.split('.')[1]=='csv']
16+
# open file to write
17+
withopen(resultfile,'w') aswritefile:
18+
forcsvfileincsvfiles:
19+
withopen(csvfile) asreadfile:
20+
print('File{} readed.'.format(csvfile))
21+
# do the read and write
22+
writefile.write(readfile.read()+'\n')
23+
print('\nFile{} writed.'.format(resultfile))
724

8-
defmain():
9-
directory= []
10-
fordirsinos.walk("."):
11-
directory.append(dirs)
12-
folders=directory[0][1]
13-
forffinfolders:
14-
ifff!=".git":
15-
allFiles=glob.glob(ff+"/*.csv")
16-
frame=pd.DataFrame()
17-
dfs= []
18-
forfilesinallFiles:
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()

0 commit comments

Comments
(0)