generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_writer.py
39 lines (30 loc) · 1.13 KB
/
file_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from typing import Any
import csv
from simple_salesforce import Salesforce
from aws_lambda_powertools import Logger
from queries import get_all_fields_for_object
logger = Logger()
def write_dict_to_csv(
session: Salesforce, dicts: list[dict[str, Any]], sobj: str
) -> None:
"""Writes a dictionary to a CSV file
Args:
dicts (list[dict[str, Any]]): list of dicts to write to a csv
filename (str): Filename to write to
"""
columns = get_all_fields_for_object(session, sobj)
logger.info(f"Writing {sobj}.csv")
with open(f"./csv/{sobj}.csv", "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=columns)
writer.writeheader()
writer.writerows(dicts)
def write_dict_to_file(dicts: list[dict[str, Any]], filename: str) -> None:
"""Writes a dictionary to a file
Args:
dicts (list[dict[str, Any]]): list of Dictionaries to write
filename (str): Filename to write to
"""
logger.info(f"Writing {filename}.txt")
with open(f"./failed/{filename}.txt", "w") as csvfile:
for data in dicts:
csvfile.write(str(data) + "\n")