lib-af-mosaic/lib_afc_mosaic/helpers.py

42 lines
1009 B
Python
Raw Normal View History

2023-12-07 06:21:25 +00:00
"""
Helper functions for the Mosaic library
"""
import csv
import codecs
def read_voterdata_csv_stream(input_stream) -> list:
"""
read_voterdata_csv_stream: Read csv stream to list
:param input_stream: Input stream to read from
:return List of parsed csv rows
"""
rows = []
# note: using 'utf-8-sig' removes the annoying \ufeff BOM signature
reader = csv.DictReader(codecs.getreader("utf-8-sig")(input_stream))
for row in reader:
key_list = row.keys()
new_row = {}
for k in key_list:
new_row[k] = row[k]
rows.append(new_row)
return rows
def write_csv_file(full_file_pathname, fieldnames, rows):
"""
write_csv_file
:param full_file_pathname:
:param fieldnames:
"""
with open(full_file_pathname, 'w', newline='', encoding='UTF-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)