ajout script d'export de données vers CSV
permet l'applatissement du JSON. close #1
This commit is contained in:
parent
f015641a11
commit
4ab903cbd9
61
LittleBock/export.py
Executable file
61
LittleBock/export.py
Executable 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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user