-
Notifications
You must be signed in to change notification settings - Fork 69
/
csv-lookup.py
303 lines (275 loc) · 10.6 KB
/
csv-lookup.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python
__description__ = 'Tool to lookup value for CSV files'
__author__ = 'Didier Stevens'
__version__ = '0.0.1'
__date__ = '2016/12/19'
"""
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2012/12/10: start
2012/12/24: fixed pipe bug
2014/01/21: added columnValue
2014/01/26: added option exclude
2014/01/30: added option found
2014/08/13: added option unquoted, changed skipinitialspace
2015/08/13: added support for \t separator
2016/04/22: added support for .gz
2016/12/18: added ParseOptionSeparator, added option -p
2016/12/19: added option useheader
Todo:
"""
import csv
import optparse
import signal
import os
import gzip
import cStringIO
QUOTE = '"'
# 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 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 BuildDictionary(fileLookup, columnLookup, separator, header, ignorecase, unquoted):
if ignorecase:
Function = str.lower
else:
Function = lambda x:x
dLookup = {}
fIn = open(fileLookup, 'rb')
reader = csv.reader(fIn, delimiter=separator, skipinitialspace=False, quoting=IFF(unquoted, csv.QUOTE_NONE, csv.QUOTE_MINIMAL))
if header:
reader.next()
for row in reader:
if not Function(row[columnLookup]) in dLookup:
dLookup[Function(row[columnLookup])] = row
fIn.close()
return dLookup
def SelectValuesFromRow(row, columns):
result = []
for column in columns:
if column < len(row):
result.append(row[column])
else:
result.append('')
return result
def Lookup(key, dictionary, partial):
if key in dictionary:
return (dictionary[key], 'FULLMATCH', '')
if partial == '':
return (None, '', '')
splitkey = key.split(partial[0])
if partial[1] == 'l':
splitkey = splitkey[1:]
else:
splitkey = splitkey[:-1]
minimumlength = 1
if partial[2:] !='':
minimumlength = int(partial[2:])
while len(splitkey) >= minimumlength:
partialkey = partial[0].join(splitkey)
if partialkey in dictionary:
return (dictionary[partialkey], str(len(splitkey)), partialkey)
if partial[1] == 'l':
splitkey = splitkey[1:]
else:
splitkey = splitkey[:-1]
return (None, '', '')
def CSVLookup(fileCSV, columnCSV, headers, fileLookup, columnLookup, columnValues, fileOutput, options):
dLookup = BuildDictionary(fileLookup, columnLookup, options.separator[1], options.headers, options.ignorecase, options.unquoted)
if options.replace:
replace = 0
else:
replace = 1
if options.ignorecase:
Function = str.lower
else:
Function = lambda x:x
if os.path.splitext(fileCSV)[1].lower() == '.gz':
fIn = gzip.GzipFile(fileCSV, 'rb')
else:
fIn = open(fileCSV, 'rb')
if fileOutput.endswith('.gz'):
fOut = gzip.GzipFile(fileOutput, 'w')
else:
fOut = open(fileOutput, 'w')
reader = csv.reader(fIn, delimiter=options.separator[0], skipinitialspace=False, quoting=IFF(options.unquoted, csv.QUOTE_NONE, csv.QUOTE_MINIMAL))
if options.headers:
reader.next()
fOut.write(MakeCSVLine(headers, options.separator[2], QUOTE) + '\n')
for row in reader:
matchedrow, match, partialkey = Lookup(Function(row[columnCSV]), dLookup, options.partial)
if matchedrow != None:
if options.found:
value = ['1']
else:
value = SelectValuesFromRow(matchedrow, columnValues)
elif options.exclude:
value = None
else:
if options.found:
value = ['0']
else:
value = map(lambda x:'', columnValues)
if value != None:
if options.partial != '':
value.append(match)
value.append(partialkey)
out = row[0:columnCSV + replace] + value + row[columnCSV + 1:]
fOut.write(MakeCSVLine(out, options.separator[2], QUOTE) + '\n')
fIn.close()
fOut.close()
def GetHeader(file, useheader, separator, unquoted):
if useheader != '':
fIn = cStringIO.StringIO(useheader)
elif os.path.splitext(file)[1].lower() == '.gz':
fIn = gzip.GzipFile(file, 'rb')
else:
fIn = open(file, 'rb')
reader = csv.reader(fIn, delimiter=separator, skipinitialspace=False, quoting=IFF(unquoted, csv.QUOTE_NONE, csv.QUOTE_MINIMAL))
header = reader.next()
fIn.close()
return header
def ConvertHeaderToIndex(file, useheader, separator, columns, unquoted):
try:
header = GetHeader(file, useheader, separator, unquoted)
result = []
for column in columns.split(separator):
result.append(header.index(column))
return result
except:
return None
def Process(fileCSV, columnCSV, fileLookup, columnLookup, columnValue, fileOutput, options):
FixPipe()
if options.headers:
inputuseheader = ''
lookupuseheader = ''
if options.useheader != '':
if options.useheader[0] == 'l':
lookupuseheader = options.useheader[2:]
else:
inputuseheader = options.useheader[2:]
lColumnCSV = ConvertHeaderToIndex(fileCSV, inputuseheader, options.separator[0], columnCSV, options.unquoted)
if lColumnCSV == None or len(lColumnCSV) != 1:
print('Column %s not found in file %s' % (columnCSV, fileCSV))
return
iColumnCSV = lColumnCSV[0]
lColumnLookup = ConvertHeaderToIndex(fileLookup, lookupuseheader, options.separator[1], columnLookup, options.unquoted)
if lColumnLookup == None or len(lColumnLookup) != 1:
print('Column %s not found in file %s' % (columnLookup, fileLookup))
return
iColumnLookup = lColumnLookup[0]
headersCSV = GetHeader(fileCSV, inputuseheader, options.separator[0], options.unquoted)
headersLookup = GetHeader(fileLookup, lookupuseheader, options.separator[1], options.unquoted)
if options.found:
headersInsert = [columnValue]
lColumnValue = []
else:
lColumnValue = ConvertHeaderToIndex(fileLookup, lookupuseheader, options.separator[1], columnValue, options.unquoted)
if lColumnValue == None or len(lColumnValue) == 0:
print('Column %s not found in file %s' % (columnValue, fileLookup))
return
headersInsert = SelectValuesFromRow(headersLookup, lColumnValue)
if options.replace:
replace = 0
else:
replace = 1
headers = headersCSV[0:iColumnCSV + replace] + headersInsert + headersCSV[iColumnCSV + 1:]
else:
iColumnCSV = int(columnCSV)
iColumnLookup = int(columnLookup)
lColumnValue = map(int, columnValue.split(options.separator[2]))
headers = None
CSVLookup(fileCSV, iColumnCSV, headers, fileLookup, iColumnLookup, lColumnValue, fileOutput, options)
def ParseOptionSeparator(separator, number):
separator = separator.replace(r'\t', '\t')
if len(separator) == 1:
return [separator for i in range(number)]
if len(separator) != number:
print('Error: expected %d separators, %d were provided' % (number, len(separator)))
return None
return [c for c in separator]
def ParseOptionPartial(partial):
if partial == '':
return False
if len(partial) == 1:
print('Error: second character of option partial should be present')
return True
if not partial[1] in ['l', 'r']:
print('Error: second character of option partial should be l or r')
return True
if len(partial) > 2:
try:
number = int(partial[2:])
if number <= 0:
print('Error: integer of option partial should be at least 1')
return True
except:
print('Error: third character of option partial should be an integer')
return True
return False
def CheckOptionUseheader(useheader):
if useheader == '':
return False
if useheader[0:2] not in ['i:', 'l:']:
print('Error: option useheader should start with i: or l:')
return True
return False
def CheckOptions(options):
options.separator = ParseOptionSeparator(options.separator, 3)
if options.separator == None:
return True
if ParseOptionPartial(options.partial):
return True
if CheckOptionUseheader(options.useheader):
return True
return False
def Main():
oParser = optparse.OptionParser(usage='usage: %prog [options] fileCSV columnCSV fileLookup columnLookup columnValue fileOutput\n' + __description__, version='%prog ' + __version__)
oParser.add_option('-s', '--separator', default=';', help='separator character(s) (default ;)')
oParser.add_option('-H', '--headers', action='store_true', default=False, help='files have headers')
oParser.add_option('-r', '--replace', action='store_true', default=False, help='replace value')
oParser.add_option('-i', '--ignorecase', action='store_true', default=False, help='ignore case')
oParser.add_option('-e', '--exclude', action='store_true', default=False, help='exclude rows with no lookup value')
oParser.add_option('-f', '--found', action='store_true', default=False, help='use indicator: was lookup successful or not')
oParser.add_option('-p', '--partial', default='', help='settings for partial lookup')
oParser.add_option('-U', '--unquoted', action='store_true', default=False, help='No handling of quotes in CSV file')
oParser.add_option('--useheader', default='', help='Header to use for i(nput) or l(ookup) file')
(options, args) = oParser.parse_args()
if CheckOptions(options):
return
if len(args) != 6:
oParser.print_help()
print('')
print(' %s' % __description__)
return
Process(args[0], args[1], args[2], args[3], args[4], args[5], options)
if __name__ == '__main__':
Main()