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 0000000000000000000000000000000000000000..3b319dd84b5ffe93b7b3f8f08821e38982259167 --- /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 0000000000000000000000000000000000000000..a65f3b4fda903da6555592731c0dbae8ef471734 --- /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