-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-filter
143 lines (129 loc) · 3.4 KB
/
csv-filter
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
#!python
import csv
import fileinput
import sys
import getopt
helpMessage=(
"Usage csv-filter -[GLEOAaDdcn] [INPUT_FILE]\n"
"\n"
"-c n Column to check\n"
"-G n Value must be greater than n.\n"
"-L n Value must be less that n.\n"
"-E n Value must be equal to n.\n"
"-A Strongly acending.\n"
"-a n Ascending with a tolerance of n.\n"
"-D Strongly descending.\n"
"-d n Descending with a tolerance of n.\n"
"-O Handle conditions as disjunction.\n"
"-K n Skip first n lines."
)
upperLimit = None
lowerLimit = None
equalValue = None
ascTolerance = None
descTolerance = None
col = 0
disjunct = False
skip = 0
prevLine = None
def main(argv):
global upperLimit
global lowerLimit
global col
global equalValue
global disjunct
global skip
global ascTolerance
global descTolerance
global prevLine
hexMode = False
upperLimitStr = None
lowerLimitStr = None
ascToleranceStr = None
descToleranceStr = None
optlist, args = getopt.getopt(argv, 'G:L:E:Oc:K:hAa:Dd:HC')
for opt in optlist:
if opt[0] == '-L':
upperLimitStr = opt[1]
elif opt[0] == '-G':
lowerLimitStr = opt[1]
elif opt[0] == '-c':
col = int(opt[1])-1
elif opt[0] == '-E':
equalValue = opt[1]
elif opt[0] == "-O":
disjunct = True
elif opt[0] == "-K":
skipStr = int(opt[1])
elif opt[0] == "-D":
descToleranceStr = "-1"
elif opt[0] == "-d":
descToleranceStr = opt[1]
elif opt[0] == "-A":
ascToleranceStr = "-1"
elif opt[0] == "-a":
ascToleranceStr = opt[1]
elif opt[0] == "-C":
# Constant
ascToleranceStr = "0"
descToleranceStr = "0"
else:
print(helpMessage)
exit(1)
# Convert opt strings to values
if hexMode:
upperLimit = int(upperLimitStr, 16)
lowerLimit = int(lowerLimitStr, 16)
ascTolerance = int(ascToleranceStr, 16)
descTolerance = int(descToleranceStr, 16)
else:
upperLimit = float(upperLimitStr) if upperLimitStr else 0
lowerLimit = float(lowerLimitStr) if lowerLimitStr else 0
ascTolerance = float(ascToleranceStr) if ascToleranceStr else 0
descTolerance = float(descToleranceStr) if descToleranceStr else 0
source = fileinput.input(args[0]) if len(args) else sys.stdin
for line in source:
if skip > 0:
skip -= 1
continue
fields=list(csv.reader([line]))[0]
if check(fields):
print(line, end='')
prevLine = line
def check(fields):
global upperLimit
global lowerLimit
global col
global equalValue
global disjunct
global ascTolerance
global descTolerance
global prevLine
if disjunct:
if (upperLimit and upperLimit > float(fields[col])):
return True
if (lowerLimit and lowerLimit < float(fields[col])):
return True
if (equalValue and equalValue == fields[col]):
return True
return False
else:
if upperLimit and not upperLimit > float(fields[col]):
return False
if lowerLimit and not lowerLimit < float(fields[col]):
return False
if equalValue and not equalValue == fields[col]:
return False
if ascTolerance:
if ascTolerance == -1 and prevLine[col] <= float(fields[col]):
return False
elif prevLine[col] < float(fields[col]) + ascTolerance:
return False
if descTolerance:
if descTolerance == -1 and prevLine[col] >= float(fields[col]):
return False
elif prevLine[col] > float(fields[col]) - descTolerance:
return False
return True
if __name__ == "__main__":
main(sys.argv[1:])