in response to this question on stackoverflow
Installation:
$ chmod +x csvparse.py
Usage:
$ cat sample.csv | csvparse.py
Add more filters:
Modify filter()
accordingly. E.g. to filter on the first column,
change the existing condition:
if fields[0] == 'some other value':
return True
To add more conditions extend the filters in any way
you like. Here is one example derived from a decision
table ($<n> refres to field n, zero-indexed)
:
Conditions: R1 R2 R3 R_else
$0 foo abc <any> <else>
$1 !xyz !xyz xyz
Actions:
include X X
exclude X X
def filter(fields, line):
# R_else
should_include = False
# R1
if fields[0] == "foo" and fields[1] != "xyz":
should_include = True
# R2
if fields[0] == "abc" and fields[1] != "xyz":
should_include = True
# R3
if fields[1] == "xyz":
should_include = False
return should_include
Note you could also write the same as a simple conditional statement, however this becomes unmaintainable quickly.
return (fields[0] in ['foo', 'abc']) and fields[1] != "xyz"