-
Notifications
You must be signed in to change notification settings - Fork 3
/
aggregate_csv.py
61 lines (43 loc) · 1.59 KB
/
aggregate_csv.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
54
55
56
57
58
59
60
import sys
import argparse
import re
import pymongo
from numpy import array, random
import utils
def main():
parser = argparse.ArgumentParser(description = 'Aggregating multiple files into a sinagle file.')
parser.add_argument('-f', action = 'append', help = 'The CSV files.')
parser.add_argument('-e', help = 'The flag to keep common header of files.')
parser.add_argument('-s', help = 'The flag to pad a space line between the content of files')
parser.add_argument('-o', help = 'The output file.')
args = parser.parse_args()
output = read_data(args)
if (args.o != None):
utils.write_file(output, args.o)
def read_data(args):
data_sources = []
header = ''
if (args.f != None):
if not isinstance(args.f, basestring):
parts = []
for afile in args.f:
part_of_data = utils.load_file(afile)
if args.e != None and args.e == 'y':
if header == '':
header = part_of_data[0]
part_of_data = part_of_data[1:len(part_of_data)]
parts.append(part_of_data.tolist())
if args.s != None and args.s == 'y':
parts.append("\n")
parts = [item for sublist in parts for item in sublist]
data_sources = array(parts)
else:
data_sources = utils.load_file(args.f)
output = []
if header != '':
output.append(header)
for item in data_sources:
output.append(item)
return output
if __name__ == "__main__":
main()