From 923839c77fda79da782286113128ee3586bb0d7f Mon Sep 17 00:00:00 2001 From: frabad Date: Fri, 6 May 2022 21:54:32 +0200 Subject: [PATCH] =?UTF-8?q?r=C3=A9cup=20suffixe=20et=20nettoyage=20adition?= =?UTF-8?q?nel=20des=20donn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LittleBock/extract.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/LittleBock/extract.py b/LittleBock/extract.py index 5c3d0ad..4ec09a9 100755 --- a/LittleBock/extract.py +++ b/LittleBock/extract.py @@ -2,24 +2,27 @@ """ extracteur de données iSpindel JSON entreposées par Little Bock en HTML """ -import json +import json, pathlib try: import lxml.html as LX except ModuleNotFoundError: import sys sys.exit("Le module 'lxml' est nécessaire.\n\thttp://pypi.org/lxml") -import pathlib def jsproc(data): - """nettoie des données récursivement""" - unwanted = ["color","opacity","yAxis","dashStyle"] - for u in unwanted: - if u in data.keys(): - del(data[u]) - for k,v in data.items(): - if isinstance(v,dict): - jsproc(v) - return data + """supprime les données inutiles d'une série""" + unwanted = ("color","opacity","yAxis","dashStyle", "tooltip",) + if data.get("data"): + for u in unwanted: + if u in data.keys(): + if u == "tooltip": + tt = data.get(u) + suffix = tt.get("valueSuffix") + if suffix: data.update({ + "name": " ".join([data["name"], suffix]) + }) + del(data[u]) + return data def hproc(path_in): """traite un document HTML""" @@ -29,7 +32,8 @@ def hproc(path_in): if x: data1 = x[0].get('data-chart-options') if data1: for i in json.loads(data1).pop('series'): - data2.append(jsproc(i)) + data = jsproc(i) + if data: data2.append(data) if len(data2) > 0: path_out = path_in.with_suffix('.json') with open(path_out,'w') as f: