Skip to content

Commit

Permalink
New function to build dictionary for filtered columns on abridged data
Browse files Browse the repository at this point in the history
  • Loading branch information
meddlin committed Feb 26, 2024
1 parent 2af7369 commit 85bbe99
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 17 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,21 @@ def bold_header(ws):
for c in ascii_uppercase:
ws[f'{c}1'].font = Font(bold=True)


def build_dict(row, filter_cols) -> dict[str, str]:
"""Return a new dictionary of items from the row, that are in the filter columns."""
# NOTE: https://www.guru99.com/python-dictionary-append.html

data = {}

for key in row.keys():
if key in filter_cols:
data[key] = row[key]

return data

# This was helpful: https://stackoverflow.com/questions/37182528/how-to-append-data-using-openpyxl-python-to-excel-file-from-a-specified-row
def read_csv(filename: str) -> dict[str, dict[str, str]]:
def read_csv(filename: str, filter_cols: list[str]) -> dict[str, dict[str, str]]:
""" Read CSV into two dictionaries. A full and abridged version. """
full = []
abridged = []
Expand All @@ -61,11 +74,7 @@ def read_csv(filename: str) -> dict[str, dict[str, str]]:
reader = csv.DictReader(file)
for row in reader:
full.append(row)
abridged.append({
'Posting Date': row['Posting Date'],
'Amount': row['Amount'],
'Description': row['Description']
})
abridged.append(build_dict(row, filter_cols))

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

Expand Down Expand Up @@ -140,7 +149,8 @@ def main():
arg_sheet = args.arg_sheet
arg_filter_cols = args.arg_filter_cols

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

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_row_filtered(row: dict[str, str], column_names: list[str], filter_column

return data

def format_filter_cols(filter_cs_list: str):
def format_filter_cols(filter_cs_list: str) -> list[str]:
"""Separate filter columns.
Remove leading and trailiing whitespace from filter columns.
Return as a list.
Expand Down

0 comments on commit 85bbe99

Please sign in to comment.