-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (67 loc) · 2.24 KB
/
main.py
File metadata and controls
80 lines (67 loc) · 2.24 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from openpyxl import load_workbook # type: ignore
from typing import List
class Worksheet:
def __init__(self) -> None:
self.sheet_items: List[dict] = []
def __repr__(self) -> str:
return str(self.sheet_items)
def xlsx_to_dict(self, path, select_sheet=None):
"""
Read a Worksheet and return it as array of dictionaries
:param self: Worksheet Object
:param path: Path to XLSX file
:param select_sheet: Set active sheet by name
:return: Array of rows as dictionaries
"""
book = load_workbook(path)
if select_sheet is None:
sheet = book.active
else:
sheet = book[select_sheet]
rows = sheet.max_row
cols = sheet.max_column
self.sheet_items = []
def item(row, col):
return (
sheet.cell(row=1, column=col).value,
str(sheet.cell(row=row, column=col).value),
)
for row in range(2, rows + 1):
self.sheet_items.append(
dict(item(row, col) for col in range(1, cols + 1))
)
return self
def csv_to_dict(self, csv_file, delimiter=","):
"""
Read a CSV and return it as array of dictionaries
:param self: CSV Object
:param csv_file: CSV file
:param delimiter: CSV delimiter fe. ';', ','
:return: Array of rows as dictionaries
"""
import csv
self.sheet_items = []
dict_reader = csv.DictReader(csv_file, delimiter=delimiter)
self.sheet_items = list(dict_reader)
return self.sheet_items
@property
def header(self):
"""
Return first row of data as dictionary
:return: first row of data as dictionary
"""
return self.sheet_items[0]
@property
def sanitize_sheet_items(self):
"""
Remove None or empty keys from dictionaries
:param self: XLSX or CSV Object
:return: Array of rows as dictionaries
"""
sanitized_items = self.sheet_items
for item in sanitized_items:
if None in item:
del item[None]
elif "" in item:
del item[""]
return sanitized_items