Skip to content

Commit 128cffd

Browse files
committed
refactor parser:csv parser
1 parent 5132d00 commit 128cffd

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

lambdas/shared/src/common/validator/parsers/csv_line_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ def parse_csv_line(self, csv_row: str, csv_header: str) -> None:
3030

3131
# Retrieves the value of a specific column name as a list.
3232
def get_key_value(self, field_name: str) -> list[str]:
33-
data = [self.csv_file_data[field_name]]
34-
return data
33+
retrieve_column_data = [self.csv_file_data[field_name]]
34+
return retrieve_column_data
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
# CSV importer and data access
1+
"""
2+
Reads and parses the specified CSV file into a dictionary of lists.
3+
4+
Each key in the dictionary corresponds to a CSV column header, and
5+
each value is a list containing all entries under that header.
6+
7+
csv_filename (str): The path to the CSV file to parse.
8+
delimiter (str): The field delimiter used in the CSV file. Defaults to '|'.
9+
"""
10+
211
import csv
312

413

514
class CSVParser:
6-
"""File Management"""
7-
8-
# parser variables
915
def __init__(self):
1016
self.csv_file_data = {}
1117

1218
# parse the CSV into a Dictionary
13-
def parse_csv_file(self, csv_filename):
14-
input_file = csv.DictReader(open(csv_filename))
15-
input_file = csv.DictReader(open(csv_filename), delimiter="|")
16-
self.csv_file_data = {elem: [] for elem in input_file.fieldnames}
17-
keys = self.csv_file_data.keys()
18-
for row in input_file:
19-
for key in keys:
20-
self.csv_file_data[key].append(row[key])
19+
def parse_csv_file(self, csv_filename: str, delimiter: str = "|") -> None:
20+
with open(csv_filename, newline="", encoding="utf-8") as file:
21+
csv_to_dict = csv.DictReader(file, delimiter=delimiter)
22+
self.csv_file_data = {headers: [] for headers in csv_to_dict.fieldnames}
23+
csv_header_to_keys = self.csv_file_data.keys()
24+
for row in csv_to_dict:
25+
for key in csv_header_to_keys:
26+
self.csv_file_data[key].append(row[key])
2127

22-
# ---------------------------------------------
23-
# Scan and retrieve values
2428
# retrieve a column of data to work with
25-
def get_key_value(self, field_name):
26-
# creating empty lists
27-
data = self.csv_file_data[field_name]
28-
return data
29+
def get_key_value(self, field_name: str) -> list[str]:
30+
retrieve_column_data = self.csv_file_data[field_name]
31+
return retrieve_column_data

0 commit comments

Comments
 (0)