Skip to content

Commit

Permalink
Filter columns passed in via CLI param
Browse files Browse the repository at this point in the history
- new utility function to clean a comma-separated filter list
  • Loading branch information
meddlin committed Feb 25, 2024
1 parent 4ef6bae commit e72f888
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": "--csv transactions.csv --output debug-out.xlsx --sheet filtered",
"args": "--csv transactions.csv --output debug-out.xlsx --sheet filtered --filter-cols \"'Posting Date', Amount, Description\"",
"justMyCode": false
}
]
Expand Down
8 changes: 5 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def read_csv(filename: str) -> dict[str, dict[str, str]]:

return { 'full': full, 'abridged': abridged }

def create_workbooks(full: dict[str, str], abridged: dict[str, str], sheet_name: str, output: str):
def create_workbooks(full: dict[str, str], abridged: dict[str, str], filter_columns: str, sheet_name: str, output: str):
""" Create workbooks and control what changes are made to them. (This is where the magic happens)"""

# filter_columns = ['Posting Date', 'Amount', 'Description', 'Transaction Category', 'Extended Description']
filter_columns = ['Posting Date', 'Amount', 'Description']
filter_columns = utility.format_filter_cols(filter_columns)

wb = Workbook()
del wb['Sheet'] # Delete the default sheet
Expand Down Expand Up @@ -127,14 +127,16 @@ def main():
parser.add_argument('--csv', type=str, required=True, dest='arg_csv', help="Path to .csv file to process")
parser.add_argument('--output', type=str, required=True, dest='arg_output', help="Output path for resulting .xlsx file")
parser.add_argument('--sheet', type=str, required=True, dest='arg_sheet', help="Worksheet name where filtered data will land")
parser.add_argument('--filter-cols', type=str, required=True, dest='arg_filter_cols', help="comma-separated list of columns to INCLUDE on new worksheet, other columns are left behind on 'raw' worksheet")

args = parser.parse_args()
arg_csv = args.arg_csv
arg_output = args.arg_output
arg_sheet = args.arg_sheet
arg_filter_cols = args.arg_filter_cols

datasheets = read_csv(arg_csv)
create_workbooks(full=datasheets['full'], abridged=datasheets['abridged'], sheet_name=arg_sheet, output=arg_output)
create_workbooks(full=datasheets['full'], abridged=datasheets['abridged'], filter_columns=arg_filter_cols, sheet_name=arg_sheet, output=arg_output)

if __name__ == "__main__":
main()
17 changes: 15 additions & 2 deletions utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ def get_row(row: dict[str, str], column_names: list[str]):
return data

def get_row_filtered(row: dict[str, str], column_names: list[str], filter_columns: list[str]):
""" """

data = []

for col in column_names:
if col in filter_columns:
data.append(row[col])

return data
return data

def format_filter_cols(filter_cs_list: str):
""" Separate filter columns.
Remove leading and trailiing whitespace from filter columns.
Return as a list.
"""
cols = filter_cs_list.split(',')
cleaned = []
for col in cols:
cleaned.append(col.strip().replace("'", ""))

return cleaned

0 comments on commit e72f888

Please sign in to comment.