-
Notifications
You must be signed in to change notification settings - Fork 69
/
csv-cut.py
216 lines (191 loc) · 6.27 KB
/
csv-cut.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
__description__ = 'Tool to select columns'
__author__ = 'Didier Stevens'
__version__ = '0.0.1'
__date__ = '2015/08/13'
"""
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2014/02/14: start
2014/08/03: added KeyboardInterrupt, stdin
2014/08/04: added option unquoted
2014/08/13: changed skipinitialspace, fixed header printing bug
2015/08/13: Columns now uses separator specified by separator option; added support for \t separator
Todo:
"""
import csv
import optparse
import os
import gzip
import glob
import signal
import collections
import sys
QUOTE = '"'
def FixPipe():
try:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except:
pass
def ToString(value):
if type(value) == type(''):
return value
else:
return str(value)
def Quote(value, separator, quote):
value = ToString(value)
if separator in value:
return quote + value + quote
else:
return value
def MakeCSVLine(row, separator, quote):
return separator.join([Quote(value, separator, quote) for value in row])
def Print(line, f):
if f == None:
print(line)
else:
f.write(line +'\n')
# CIC: Call If Callable
def CIC(expression):
if callable(expression):
return expression()
else:
return expression
# IFF: IF Function
def IFF(expression, valueTrue, valueFalse):
if expression:
return CIC(valueTrue)
else:
return CIC(valueFalse)
def FixPipe():
try:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except:
pass
def File2Strings(filename):
try:
f = open(filename, 'r')
except:
return None
try:
return map(lambda line:line.rstrip('\n'), f.readlines())
except:
return None
finally:
f.close()
def ProcessAt(argument):
if argument.startswith('@'):
strings = File2Strings(argument[1:])
if strings == None:
raise Exception('Error reading %s' % argument)
else:
return strings
else:
return [argument]
def ExpandFilenameArguments(filenames):
return list(collections.OrderedDict.fromkeys(sum(map(glob.glob, sum(map(ProcessAt, filenames), [])), [])))
def PrintDictionary(dCount, dSelections, sortDescending, sortKeys, totals, nocounters, separator, output, uniques, minmax):
if uniques:
listCount = [(key, len(value)) for key, value in dCount.items()]
elif minmax:
listCount = [(key, [len(value), min(value), max(value)]) for key, value in dCount.items()]
else:
listCount = [(key, value) for key, value in dCount.items()]
if sortKeys:
index = 0
else:
index = 1
listCount.sort(lambda x, y:cmp(x[index], y[index]), reverse=sortDescending)
sumValues = 0
if output:
fOut = open(output, 'w')
else:
fOut = None
for key, value in listCount:
if nocounters:
row = dSelections[key]
else:
if minmax:
row = dSelections[key] + value
else:
row = dSelections[key] + [value]
Print(MakeCSVLine(row, separator, QUOTE), fOut)
if not minmax:
sumValues += value
if totals:
Print(MakeCSVLine(['uniques', len(dCount.keys())], separator, QUOTE), fOut)
Print(MakeCSVLine(['total', sumValues], separator, QUOTE), fOut)
if output:
fOut.close()
def ConvertHeaderToIndex(header, separator, columns):
try:
result = []
for column in columns.split(separator):
result.append(header.index(column))
return result
except:
return None
def CSVCut(columns, files, options):
FixPipe()
columnsToProcess = [column for column in columns.split(options.separator)]
columnsIndices = None
if not options.header:
columnsIndices = [int(columnIndex) for columnIndex in columnsToProcess]
if options.output:
fOut = open(options.output, 'w')
else:
fOut = None
headerPrinted = False
for file in files:
if file == '':
fIn = sys.stdin
elif os.path.splitext(file)[1].lower() == '.gz':
fIn = gzip.GzipFile(file, 'rb')
else:
fIn = open(file, 'rb')
reader = csv.reader(fIn, delimiter=options.separator, skipinitialspace=False, quoting=IFF(options.unquoted, csv.QUOTE_NONE, csv.QUOTE_MINIMAL))
firstRow = True
for row in reader:
try:
if options.header and firstRow:
firstRow = False
columnsIndices = ConvertHeaderToIndex(row, options.separator, columns)
if columnsIndices == None:
print('Columns %s not found in file %s' % (columns, file))
return
if not headerPrinted:
Print(MakeCSVLine([row[columnsIndex] for columnsIndex in columnsIndices], options.separator, QUOTE), fOut)
headerPrinted = True
continue
Print(MakeCSVLine([row[columnsIndex] for columnsIndex in columnsIndices], options.separator, QUOTE), fOut)
except KeyboardInterrupt:
raise
except:
pass
if fIn != sys.stdin:
fIn.close()
if fOut:
fOut.close()
def Main():
oParser = optparse.OptionParser(usage='usage: %prog [options] columns files\n' + __description__, version='%prog ' + __version__)
oParser.add_option('-s', '--separator', default=';', help='Separator character (default ;)')
oParser.add_option('-o', '--output', help='Output to file')
oParser.add_option('-H', '--header', action='store_true', default=False, help='Header')
oParser.add_option('-U', '--unquoted', action='store_true', default=False, help='No handling of quotes in CSV file')
(options, args) = oParser.parse_args()
if options.separator == r'\t':
options.separator = '\t'
if len(args) == 0:
oParser.print_help()
print('')
print(' %s' % __description__)
return
elif len(args) == 1:
files = ['']
else:
files = ExpandFilenameArguments(args[1:])
CSVCut(args[0], files, options)
if __name__ == '__main__':
Main()