docstrings + correction du nom de doc traité

This commit is contained in:
frabad 2022-05-18 18:55:36 +02:00
parent a108d34b37
commit 7353886d9b
1 changed files with 18 additions and 14 deletions

View File

@ -1,14 +1,16 @@
#!/usr/bin/python3
"""
exportation de données iSpindel JSON vers données CSV
exportation de données iSpindel JSON vers données applaties CSV
"""
import json, pathlib, csv
import csv
import json
import pathlib
class Frame(object):
""""""
"""un cadre de données"""
def __init__(self,fname):
""""""
"""constructeur"""
with open(fname,'r') as f:
self._json = json.load(f)
self.names = tuple(self._names())
@ -16,20 +18,20 @@ class Frame(object):
self.rows = tuple(self._rows())
def _names(self):
""""""
"""génère une liste de noms"""
for group in self._json:
yield group["name"]
def _keys(self):
""""""
"""génère un liste de clés"""
for i in self._json[0]["data"]:
yield i["x"]
def _rows(self):
""""""
"""génère les lignes d'un tableau"""
def row(key):
""""""
"""génère une ligne d'un tableau"""
for name in self.names:
yield self.get(name,key)
@ -37,7 +39,7 @@ class Frame(object):
yield tuple(row(k))
def get(self,name,key):
""""""
"""récupère la valeur d'une entrée d'après son nom et sa clé"""
for group in self._json:
if group["name"] == name:
for value in group["data"]:
@ -46,16 +48,18 @@ class Frame(object):
if __name__ == "__main__":
here = pathlib.Path.cwd()
docs = tuple(here.rglob("2.json"))
docs = tuple(here.rglob("*.json"))
if len(docs) == 0:
print("Aucun fichier JSON ('.json') trouvé.")
for doc in docs:
for doc_i 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:
if doc_i.exists() and doc_i.stat().st_size > 0:
frame = Frame(doc_i)
doc_o = doc_i.with_suffix(".csv")
with open(doc_o,'w', newline='') as f:
writer = csv.writer(f,dialect='unix')
writer.writerow(frame.names)
writer.writerows(frame.rows)
print(f"INFO: {doc_i.name} >> {doc_o.name}")