CSV Loader
This commit is contained in:
48
loader_1_simple.py
Normal file
48
loader_1_simple.py
Normal file
@ -0,0 +1,48 @@
|
||||
import csv
|
||||
from enum import IntEnum
|
||||
from lib.csv_loader import CSVLoader, CSVRow
|
||||
from lib.csv_loader.csv_loader import BoolValuePair, CSVRowDefaultConfig
|
||||
|
||||
|
||||
class ActionEnum(IntEnum):
|
||||
INSERT = 0
|
||||
UPDATE = 1
|
||||
DELETE = 2
|
||||
|
||||
|
||||
class AssetRow(CSVRow):
|
||||
asset_id: str
|
||||
asset_action: ActionEnum
|
||||
serial_number: str
|
||||
brand_id: int
|
||||
model_id: int
|
||||
is_active: bool
|
||||
|
||||
class Config(CSVRowDefaultConfig):
|
||||
bool_value_pair = BoolValuePair(true="YES", false="NO")
|
||||
|
||||
|
||||
def main():
|
||||
with open("csv/assets.csv", "r") as f:
|
||||
reader = csv.reader(f, delimiter=";")
|
||||
csv_loader = CSVLoader[AssetRow](
|
||||
reader=reader,
|
||||
output_model_cls=AssetRow,
|
||||
has_header=True,
|
||||
aggregate_errors=True,
|
||||
)
|
||||
|
||||
result = csv_loader.read_rows()
|
||||
|
||||
"Results:"
|
||||
for row in result.rows:
|
||||
print(row)
|
||||
|
||||
if result.has_errors():
|
||||
print("Errors:")
|
||||
for error in result.errors:
|
||||
print(f"Line: {error.line_number}: {error.original_error}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user