Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV preprocessing fix (unquoted newlines) in expenditure data for NCSBE #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python

# import pandas as pd
from pathlib import Path
import csv
import cytoolz.curried as cc
import itertools as it
from pprint import pp

# csv_path = list(Path.cwd().glob('*.csv'))[0]

# csv_df = pd.read_csv(csv_path)

# cleaned_df = pd.DataFrame(data=csv_lst_combined, columns=csv_lst[0])


def read_expenditure_csv(csv_path):
csv_lst = []
with open(csv_path, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(spamreader)
for row in spamreader:
csv_lst.append(row)

csv_lst_paired = cc.pipe(
csv_lst
, lambda lst: (lst, cc.drop(1, [x for x in lst]))
, lambda pair: it.zip_longest(*pair)
, list
)

return csv_lst_paired


def cleanup_expenditure_csv(csv_pair_list):
csv_lst_combined = []
for row_pair in csv_pair_list:
if row_pair[0] is None:
csv_lst_combined.append(row_pair[1])
elif row_pair[1] is None:
csv_lst_combined.append(row_pair[0])
elif len(row_pair[0]) == 24:
csv_lst_combined.append(row_pair[0])
elif 15 < len(row_pair[0]) < 24:
combined_lst = (row_pair[0] + row_pair[1])[:-1]
csv_lst_combined.append(combined_lst)

return csv_lst_combined