Compare commits

..

No commits in common. "c9bc3077119b1d3f68eaac70f9f3e054bd93e8d9" and "a108d34b375c952a341f71b365bcca4f250950af" have entirely different histories.

2 changed files with 14 additions and 20 deletions

2
.gitignore vendored
View File

@ -1,6 +1,4 @@
local/ local/
*.tar.gz *.tar.gz
*.tar.xz
*.json *.json
*.csv

View File

@ -1,16 +1,14 @@
#!/usr/bin/python3 #!/usr/bin/python3
""" """
exportation de données iSpindel JSON vers données applaties CSV exportation de données iSpindel JSON vers données CSV
""" """
import csv import json, pathlib, 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())
@ -18,20 +16,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)
@ -39,7 +37,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"]:
@ -48,18 +46,16 @@ class Frame(object):
if __name__ == "__main__": if __name__ == "__main__":
here = pathlib.Path.cwd() here = pathlib.Path.cwd()
docs = tuple(here.rglob("*.json")) docs = tuple(here.rglob("2.json"))
if len(docs) == 0: if len(docs) == 0:
print("Aucun fichier JSON ('.json') trouvé.") print("Aucun fichier JSON ('.json') trouvé.")
for doc_i in docs: for doc in docs:
frame = None frame = None
if doc_i.exists() and doc_i.stat().st_size > 0: if doc.exists() and doc.stat().st_size > 0:
frame = Frame(doc_i) frame = Frame(doc)
doc_o = doc_i.with_suffix(".csv") with open(doc.with_suffix(".csv"),'w', newline='') as f:
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}")