-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_files.py
53 lines (46 loc) · 2.12 KB
/
append_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
import csv
import argparse
def append_files(files,outfile,delimiters=[',']):
'''
Append together multiple files with same or different schemas.
Usage: append_files([FileA,FileB,FileC],[DelimiterA,DelimiterB,DelimiterC],outfile)
If all the delimtiers are same, pass only one delimiter: append_files([FileA,FileB,FileC],[Delimiter],outfile)
Order out output columns: Sequential order of header in the input file list
'''
# Define Output Writer
outfile1 = open(outfile,'wb')
writer = csv.writer(outfile1)
# Read all the headers and make a master header
master_header = []
if len(delimiters) <= len(files):
delimiters = [delimiters[0] for x in files]
for file,delimiter in zip(files,delimiters):
with open(file,'rb') as fp:
header = csv.reader(fp,delimiter=delimiter).next()
master_header += filter(lambda x: x not in master_header,header)
# Write out the master header
writer.writerow(master_header)
# Iteratively read the files and append them to the output file after re-ordering the columns
for file,delimiter in zip(files,delimiters):
with open(file,'rb') as fp:
# Read header of the input file
reader = csv.reader(fp,delimiter=delimiter)
header = reader.next()
len_header = len(header)
header = dict(zip(header,range(len(header))))
for row in reader:
# Add null string for columns not present in this file
row.append('')
# Re-order the columns
output_row = [row[header.get(column,len_header)] for column in master_header]
writer.writerow(output_row)
parser = argparse.ArgumentParser()
parser.add_argument("--ifile", help="Input files split by |")
parser.add_argument("--ofile", help="Output file")
parser.add_argument("--d", help="Delimiters of input file split by ~. Default: Comma")
args = parser.parse_args()
if args.d:
append_files(args.ifile.split('|'),args.ofile,args.d.split('~'))
else:
append_files(args.ifile.split('|'),args.ofile)