From c66b56c76b976f429a235445eed9ffffb3b3bfe2 Mon Sep 17 00:00:00 2001 From: Eric Duminil <eric.duminil@gmail.com> Date: Tue, 23 Jul 2024 13:13:19 +0200 Subject: [PATCH] Try to convert CSV to greendb.xml --- .../csv_to_green_db/convert_to_greendb.py | 35 +++++++++++++++++++ .../csv_to_green_db/input/greendb_example.csv | 2 ++ 2 files changed, 37 insertions(+) create mode 100644 python_scripts/csv_to_green_db/convert_to_greendb.py create mode 100644 python_scripts/csv_to_green_db/input/greendb_example.csv diff --git a/python_scripts/csv_to_green_db/convert_to_greendb.py b/python_scripts/csv_to_green_db/convert_to_greendb.py new file mode 100644 index 0000000..3b319dd --- /dev/null +++ b/python_scripts/csv_to_green_db/convert_to_greendb.py @@ -0,0 +1,35 @@ +from pathlib import Path +from dicttoxml import dicttoxml +from xml.dom.minidom import parseString + +SCRIPT_DIR = Path(__file__).resolve().parent +INPUT_DIR = SCRIPT_DIR / 'input' +OUTPUT_DIR = SCRIPT_DIR / 'output' +OUTPUT_DIR.mkdir(exist_ok=True) + +csv_path = INPUT_DIR / 'greendb_example.csv' + +trees = [] + +db = { + 'greenDatabase': { + 'name': 'Gründatenbank Prototyp', + 'description': f'Gründatenbank exported from {csv_path.name}', + 'trees': trees + } +} + +with open(csv_path) as csv: + for line in csv: + name, factor, *aliases = line.strip().split(';') + trees.append( + {'tree': {'name': name, + 'speciesFactor': factor, + 'aliases': [{'alias': alias} for alias in aliases] + } + } + ) + +xml = dicttoxml(db, attr_type=False, root=False) +dom = parseString(xml) +print(dom.toprettyxml()) diff --git a/python_scripts/csv_to_green_db/input/greendb_example.csv b/python_scripts/csv_to_green_db/input/greendb_example.csv new file mode 100644 index 0000000..a65f3b4 --- /dev/null +++ b/python_scripts/csv_to_green_db/input/greendb_example.csv @@ -0,0 +1,2 @@ +Acer platanoides;0.5;Acer platanoides Columnare Ty;Acer platanoides Klimabaum; +Quercus robur;0.5;Blablabla;blabalblabal2;blabalblabal3 -- GitLab