|
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 | + |
2 | 11 | import csv |
3 | 12 |
|
4 | 13 |
|
5 | 14 | class CSVParser: |
6 | | - """File Management""" |
7 | | - |
8 | | - # parser variables |
9 | 15 | def __init__(self): |
10 | 16 | self.csv_file_data = {} |
11 | 17 |
|
12 | 18 | # 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]) |
21 | 27 |
|
22 | | - # --------------------------------------------- |
23 | | - # Scan and retrieve values |
24 | 28 | # 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