-
Notifications
You must be signed in to change notification settings - Fork 4
Import laydgeur questions
Raphael Odini edited this page Nov 22, 2020
·
1 revision
import csv
from datetime import datetime, date
"""
laydgeur file
# N°;Question;URL question;Catégorie;Sous catégorie énergie;Réponse 1;Réponse 2;Réponse 3;Réponse 4;Numéro de la bonne réponse;% réponse 1;% réponse 2;% réponse 3;% réponse 4;Réponse qui a eu le plus de votes;Nb Votes;Nb RT;Nb Like;Date;Heure;Réponse;Source;Complément source;Commentaire;Somme % réponses;Nb Réponses;Impressions;Engagement totaux;Nb Votes Loess 10;Réponse correcte ?;Date début de mois
"""
LAYDGEUR_CATEGORY_MAPPING = {
"Energie": "Énergie",
"Climat": "Climat",
"Divertissement": "Autres",
"Transport": "Autres",
"AFOLU": "Autres",
"Emissions": "Autres"
}
LAYDGEUR_ANSWER_CORRECT_MAPPING = {
"1": "a",
"2": "b",
"3": "c",
"4": "d"
}
def clean_csv(filename):
with open(filename + '.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';')
with open(filename + '_cleaned_above_123.csv', 'w', encoding='utf-8') as csvcleanedfile:
print(reader.fieldnames)
# fieldnames = reader.fieldnames
fieldnames = ["id", "text", "type", "category", "answer_option_a", "answer_option_b", "answer_option_c", "answer_option_d", "answer_correct", "answer_explanation", "answer_scientific_url", "answer_image_url", "answer_extra_info", "author", "added", "validation_status"]
writer = csv.DictWriter(csvcleanedfile, delimiter=',', fieldnames=fieldnames)
writer.writeheader()
for row in reader:
# writer_row = row
writer_row = {}
# if int(row["id"]) > 83:
# row["tags"] = row["tags"].replace(";", ",")
# row["answer_correct"] = row["answer_correct"].lower()
# row["type"] = "QCM"
# print(row)
# writer.writerow(row)
if int(row["N°"]) > 123:
writer_row["id"] = int(row["N°"]) + 116
writer_row["text"] = row["Question"]
writer_row["type"] = "QCM"
writer_row["category"] = LAYDGEUR_CATEGORY_MAPPING[row["Catégorie"]]
writer_row["answer_option_a"] = row["Réponse 1"]
writer_row["answer_option_b"] = row["Réponse 2"]
writer_row["answer_option_c"] = row["Réponse 3"]
writer_row["answer_option_d"] = row["Réponse 4"]
writer_row["answer_correct"] = LAYDGEUR_ANSWER_CORRECT_MAPPING[row["Numéro de la bonne réponse"]]
writer_row["answer_explanation"] = row["Réponse"]
writer_row["answer_scientific_url"] = row["Source"]
writer_row["answer_image_url"] = "https://raw.githubusercontent.com/raphodn/know-your-planet/master/api/static/images/laydgeur/r" + row["N°"] + ".jpg"
writer_row["answer_extra_info"] = ((row["Commentaire"] + "\n") if row["Commentaire"] != "" else "") + row["URL question"]
writer_row["author"] = "laydgeur"
writer_row["added"] = row["Date"] # datetime.strptime(row["Date"], "%d/%m/%y").date()
writer_row["validation_status"] = "Nouvelle question"
writer.writerow(writer_row)
"""
main()
"""
if __name__ == '__main__':
"""
python local_scripts/load_questions_csv.py
"""
filename = "local_data/laydgeur/Masterfile Quizz 2020-04-30"
clean_csv(filename)