récup suffixe et nettoyage aditionnel des données

This commit is contained in:
frabad 2022-05-06 21:54:32 +02:00
parent 161b32bacf
commit 923839c77f
1 changed files with 16 additions and 12 deletions

View File

@ -2,24 +2,27 @@
""" """
extracteur de données iSpindel JSON entreposées par Little Bock en HTML extracteur de données iSpindel JSON entreposées par Little Bock en HTML
""" """
import json import json, pathlib
try: try:
import lxml.html as LX import lxml.html as LX
except ModuleNotFoundError: except ModuleNotFoundError:
import sys import sys
sys.exit("Le module 'lxml' est nécessaire.\n\thttp://pypi.org/lxml") sys.exit("Le module 'lxml' est nécessaire.\n\thttp://pypi.org/lxml")
import pathlib
def jsproc(data): def jsproc(data):
"""nettoie des données récursivement""" """supprime les données inutiles d'une série"""
unwanted = ["color","opacity","yAxis","dashStyle"] unwanted = ("color","opacity","yAxis","dashStyle", "tooltip",)
for u in unwanted: if data.get("data"):
if u in data.keys(): for u in unwanted:
del(data[u]) if u in data.keys():
for k,v in data.items(): if u == "tooltip":
if isinstance(v,dict): tt = data.get(u)
jsproc(v) suffix = tt.get("valueSuffix")
return data if suffix: data.update({
"name": " ".join([data["name"], suffix])
})
del(data[u])
return data
def hproc(path_in): def hproc(path_in):
"""traite un document HTML""" """traite un document HTML"""
@ -29,7 +32,8 @@ def hproc(path_in):
if x: data1 = x[0].get('data-chart-options') if x: data1 = x[0].get('data-chart-options')
if data1: if data1:
for i in json.loads(data1).pop('series'): for i in json.loads(data1).pop('series'):
data2.append(jsproc(i)) data = jsproc(i)
if data: data2.append(data)
if len(data2) > 0: if len(data2) > 0:
path_out = path_in.with_suffix('.json') path_out = path_in.with_suffix('.json')
with open(path_out,'w') as f: with open(path_out,'w') as f: