Compare commits

...

2 Commits

Author SHA1 Message Date
frabad a108d34b37 +commentaires 2022-05-18 18:29:57 +02:00
frabad 4ab903cbd9 ajout script d'export de données vers CSV
permet l'applatissement du JSON. close #1
2022-05-18 18:25:21 +02:00
2 changed files with 68 additions and 6 deletions

View File

@ -7,6 +7,8 @@ import datetime
"""
ajoute une valeur de date lisible par un humain à chaque entrée d'un document JSON
TODO: rassembler les dates dans un groupe d'entrées
"""
def from_stamp(i: int) -> str:
@ -15,10 +17,9 @@ def from_stamp(i: int) -> str:
if len(dt)>10:
dt = dt[:10]
sdt = str(datetime.datetime.fromtimestamp(int(dt)))
#print(f" {dt} : {sdt}")
return sdt
def jsproc(path_in):
def proc(path_in):
"""traite un document JSON"""
_json = None
with open(path_in,'r') as f:
@ -30,12 +31,12 @@ def jsproc(path_in):
if __name__ == "__main__":
here = pathlib.Path.cwd()
jsdocs = tuple(here.rglob("*.json"))
if len(jsdocs) == 0:
docs = tuple(here.rglob("*.json"))
if len(docs) == 0:
print("Aucun fichier JSON ('.json') trouvé.")
for i in jsdocs:
for i in docs:
if i.exists() and i.stat().st_size > 0:
data = jsproc(i)
data = proc(i)
if data:
with open(i,'w') as f:
f.write(json.dumps(data,

61
LittleBock/export.py Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/python3
"""
exportation de données iSpindel JSON vers données CSV
"""
import json, pathlib, csv
class Frame(object):
""""""
def __init__(self,fname):
""""""
with open(fname,'r') as f:
self._json = json.load(f)
self.names = tuple(self._names())
self.keys = tuple(self._keys())
self.rows = tuple(self._rows())
def _names(self):
""""""
for group in self._json:
yield group["name"]
def _keys(self):
""""""
for i in self._json[0]["data"]:
yield i["x"]
def _rows(self):
""""""
def row(key):
""""""
for name in self.names:
yield self.get(name,key)
for k in self.keys:
yield tuple(row(k))
def get(self,name,key):
""""""
for group in self._json:
if group["name"] == name:
for value in group["data"]:
if value.get("x") == key:
return value.get("y")
if __name__ == "__main__":
here = pathlib.Path.cwd()
docs = tuple(here.rglob("2.json"))
if len(docs) == 0:
print("Aucun fichier JSON ('.json') trouvé.")
for doc 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:
writer = csv.writer(f,dialect='unix')
writer.writerow(frame.names)
writer.writerows(frame.rows)