docstrings + correction du nom de doc traité
This commit is contained in:
parent
a108d34b37
commit
7353886d9b
@ -1,14 +1,16 @@
|
|||||||
#!/usr/bin/python3
|
#!/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):
|
class Frame(object):
|
||||||
""""""
|
"""un cadre de données"""
|
||||||
|
|
||||||
def __init__(self,fname):
|
def __init__(self,fname):
|
||||||
""""""
|
"""constructeur"""
|
||||||
with open(fname,'r') as f:
|
with open(fname,'r') as f:
|
||||||
self._json = json.load(f)
|
self._json = json.load(f)
|
||||||
self.names = tuple(self._names())
|
self.names = tuple(self._names())
|
||||||
@ -16,20 +18,20 @@ class Frame(object):
|
|||||||
self.rows = tuple(self._rows())
|
self.rows = tuple(self._rows())
|
||||||
|
|
||||||
def _names(self):
|
def _names(self):
|
||||||
""""""
|
"""génère une liste de noms"""
|
||||||
for group in self._json:
|
for group in self._json:
|
||||||
yield group["name"]
|
yield group["name"]
|
||||||
|
|
||||||
def _keys(self):
|
def _keys(self):
|
||||||
""""""
|
"""génère un liste de clés"""
|
||||||
for i in self._json[0]["data"]:
|
for i in self._json[0]["data"]:
|
||||||
yield i["x"]
|
yield i["x"]
|
||||||
|
|
||||||
def _rows(self):
|
def _rows(self):
|
||||||
""""""
|
"""génère les lignes d'un tableau"""
|
||||||
|
|
||||||
def row(key):
|
def row(key):
|
||||||
""""""
|
"""génère une ligne d'un tableau"""
|
||||||
for name in self.names:
|
for name in self.names:
|
||||||
yield self.get(name,key)
|
yield self.get(name,key)
|
||||||
|
|
||||||
@ -37,7 +39,7 @@ class Frame(object):
|
|||||||
yield tuple(row(k))
|
yield tuple(row(k))
|
||||||
|
|
||||||
def get(self,name,key):
|
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:
|
for group in self._json:
|
||||||
if group["name"] == name:
|
if group["name"] == name:
|
||||||
for value in group["data"]:
|
for value in group["data"]:
|
||||||
@ -46,16 +48,18 @@ class Frame(object):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
here = pathlib.Path.cwd()
|
here = pathlib.Path.cwd()
|
||||||
docs = tuple(here.rglob("2.json"))
|
docs = tuple(here.rglob("*.json"))
|
||||||
if len(docs) == 0:
|
if len(docs) == 0:
|
||||||
print("Aucun fichier JSON ('.json') trouvé.")
|
print("Aucun fichier JSON ('.json') trouvé.")
|
||||||
for doc in docs:
|
for doc_i in docs:
|
||||||
frame = None
|
frame = None
|
||||||
if doc.exists() and doc.stat().st_size > 0:
|
if doc_i.exists() and doc_i.stat().st_size > 0:
|
||||||
frame = Frame(doc)
|
frame = Frame(doc_i)
|
||||||
with open(doc.with_suffix(".csv"),'w', newline='') as f:
|
doc_o = doc_i.with_suffix(".csv")
|
||||||
|
with open(doc_o,'w', newline='') as f:
|
||||||
writer = csv.writer(f,dialect='unix')
|
writer = csv.writer(f,dialect='unix')
|
||||||
writer.writerow(frame.names)
|
writer.writerow(frame.names)
|
||||||
writer.writerows(frame.rows)
|
writer.writerows(frame.rows)
|
||||||
|
print(f"INFO: {doc_i.name} >> {doc_o.name}")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user