in response to this question on stackoverflow
$ chmod +x csvparse.py
$ cat sample.csv | csvparse.py
Add more filters:
filter() accordingly. E.g. to filter on the first column,
change the existing condition:
if fields == '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
$<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 == "foo" and fields != "xyz": should_include = True # R2 if fields == "abc" and fields != "xyz": should_include = True # R3 if fields == "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 in ['foo', 'abc']) and fields != "xyz"