Skip to content

Instantly share code, notes, and snippets.

@danker
Last active November 12, 2016 00:17
Show Gist options
  • Save danker/e9cdf14cadb691bcf0d347036fe1fcd5 to your computer and use it in GitHub Desktop.
Save danker/e9cdf14cadb691bcf0d347036fe1fcd5 to your computer and use it in GitHub Desktop.
import os, csv, sys, argparse
parser = argparse.ArgumentParser(
description='Filter the contact list. Produce one sub-list per title provided, filtering out non-desired titles.')
parser.add_argument('--filter', nargs='*', help="titles we're not interested in'")
parser.add_argument('titles', nargs='*', help='titles we *are* interested in')
args = parser.parse_args()
search_titles = args.titles
filtered_titles = args.filter
directory = "."
csv_input_file = "aws.csv"
def desired_title(title):
d_title = True
if filtered_titles: #if it matches our filter list it's NOT a desired title
for f_title in filtered_titles:
if f_title.lower() in title:
d_title = False
return d_title
##### MAIN #####
for search_title in search_titles:
csv_output_file = "reinvent_master_sheet-filtered-" + search_title + ".csv"
with open(csv_input_file, 'r', newline='') as csv_in:
with open(csv_output_file, 'w', newline='') as csv_out:
csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_reader = csv.reader(csv_in, delimiter=',', quotechar='"')
for row in csv_reader:
current_title = row[3].lower()
if (search_title.lower() in current_title) and desired_title(current_title):
csv_writer.writerow(row)
@trotter
Copy link

trotter commented Nov 12, 2016

From a purely stylistic standpoint, you've got some extra empty lines that are pretty weird. So remove lines 18, 31, 36, & 41. I'd probably remove all the empty lines that aren't separating functions, but I don't think that's strictly necessary. Run a python linter though and see what it says.

On line 6, default filtered_titles to an empty list, that way you don't need line 21. Also pretty sure it's more Pythony to use a list comprehension for lines 22 & 23, though I'm not really sure how to do that. Even so, you could delete 23 & 24 and replace them with d_title = !(f_title.lower() in title).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment