Last active
November 12, 2016 00:17
-
-
Save danker/e9cdf14cadb691bcf0d347036fe1fcd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 withd_title = !(f_title.lower() in title)
.