Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Drupal2CSV

David Larlet edited this page Jul 2, 2018 · 1 revision

Script de conversion des fichiers JSON issus de Drupal aux fichiers CSV au format répondeur:

import csv
import json
import sys
from pathlib import Path

FIELDNAMES = [
    "N°",
    "Avis du Gouvernement",
    "Objet (article / amdt)",
    "Avis et observations de l'administration référente",
]


def filter_amendements(items):
    for article in items[0]["list"]:
        for amendement in article.get("amendements", []):
            if "reponse" in amendement:
                yield amendement


def main():
    file_name = sys.argv[1]
    items = json.loads(Path(file_name).read_text())
    for amendement in filter_amendements(items):
        reponse = amendement["reponse"]
        writer = csv.DictWriter(sys.stdout, fieldnames=FIELDNAMES)
        writer.writeheader()
        writer.writerow(
            {
                "N°": amendement["idAmendement"],
                "Avis du Gouvernement": reponse["avis"],
                "Objet (article / amdt)": reponse.get("presentation", ""),
                "Avis et observations de l'administration référente": reponse.get(
                    "reponse", ""
                ),
            }
        )


main()

Usage :

python ../drupal2csv.py ../../Archives\ PLFSS\ 2018/JSON\ -\ fichier\ de\ sauvegarde/Sénat1-2018.json > ../../Archives\ PLFSS\ 2018/JSON\ -\ fichier\ de\ sauvegarde/Sénat1-2018.csv

Clone this wiki locally