From 4ab903cbd9c8bd4c09d17070ef88d08e0159cacf Mon Sep 17 00:00:00 2001 From: frabad Date: Wed, 18 May 2022 18:25:21 +0200 Subject: [PATCH] =?UTF-8?q?ajout=20script=20d'export=20de=20donn=C3=A9es?= =?UTF-8?q?=20vers=20CSV?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit permet l'applatissement du JSON. close #1 --- LittleBock/export.py | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 LittleBock/export.py diff --git a/LittleBock/export.py b/LittleBock/export.py new file mode 100755 index 0000000..258a21e --- /dev/null +++ b/LittleBock/export.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3 +""" +exportation de données iSpindel JSON vers données CSV +""" +import json, pathlib, csv + +class Frame(object): + """""" + + def __init__(self,fname): + """""" + with open(fname,'r') as f: + self._json = json.load(f) + self.names = tuple(self._names()) + self.keys = tuple(self._keys()) + self.rows = tuple(self._rows()) + + def _names(self): + """""" + for group in self._json: + yield group["name"] + + def _keys(self): + """""" + for i in self._json[0]["data"]: + yield i["x"] + + def _rows(self): + """""" + + def row(key): + """""" + for name in self.names: + yield self.get(name,key) + + for k in self.keys: + yield tuple(row(k)) + + def get(self,name,key): + """""" + for group in self._json: + if group["name"] == name: + for value in group["data"]: + if value.get("x") == key: + return value.get("y") + +if __name__ == "__main__": + here = pathlib.Path.cwd() + docs = tuple(here.rglob("2.json")) + if len(docs) == 0: + print("Aucun fichier JSON ('.json') trouvé.") + for doc in docs: + frame = None + if doc.exists() and doc.stat().st_size > 0: + frame = Frame(doc) + with open(doc.with_suffix(".csv"),'w', newline='') as f: + writer = csv.writer(f,dialect='unix') + writer.writerow(frame.names) + writer.writerows(frame.rows) + +