diff --git a/TP fichier/ages.csv b/TP fichier/ages.csv new file mode 100644 index 0000000..211333e --- /dev/null +++ b/TP fichier/ages.csv @@ -0,0 +1,7 @@ +NOM;PRENOM;AGE +Marie;Dupond;12 +Pierre;Durand;46 +Nathalie;Martin;35 +Jean;Thomas;17 +Isabelle;Dubois;9 +Nicolas;Petit;70 diff --git a/TP fichier/ages_majeurs.csv b/TP fichier/ages_majeurs.csv new file mode 100644 index 0000000..7f728da --- /dev/null +++ b/TP fichier/ages_majeurs.csv @@ -0,0 +1,4 @@ +NOM;PRENOM;AGE +Pierre;Durand;46 +Nathalie;Martin;35 +Nicolas;Petit;70 diff --git a/TP fichier/lorem_ipsum.txt b/TP fichier/lorem_ipsum.txt new file mode 100644 index 0000000..cff726b --- /dev/null +++ b/TP fichier/lorem_ipsum.txt @@ -0,0 +1,4 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Quisque placerat ullamcorper blandit. +Integer eu dignissim sapien, commodo mattis nunc. +Nulla quam felis, scelerisque ut velit sit amet, aliquam sodales velit. diff --git a/TP fichier/lorem_ipsum_copie.txt b/TP fichier/lorem_ipsum_copie.txt new file mode 100644 index 0000000..cff726b --- /dev/null +++ b/TP fichier/lorem_ipsum_copie.txt @@ -0,0 +1,4 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Quisque placerat ullamcorper blandit. +Integer eu dignissim sapien, commodo mattis nunc. +Nulla quam felis, scelerisque ut velit sit amet, aliquam sodales velit. diff --git a/TP fichier/lorem_ipsum_inv.txt b/TP fichier/lorem_ipsum_inv.txt new file mode 100644 index 0000000..7db462c --- /dev/null +++ b/TP fichier/lorem_ipsum_inv.txt @@ -0,0 +1,4 @@ +Nulla quam felis, scelerisque ut velit sit amet, aliquam sodales velit. +Integer eu dignissim sapien, commodo mattis nunc. +Quisque placerat ullamcorper blandit. +Lorem ipsum dolor sit amet, consectetur adipiscing elit. diff --git a/TP fichier/main.py b/TP fichier/main.py new file mode 100644 index 0000000..2696554 --- /dev/null +++ b/TP fichier/main.py @@ -0,0 +1,29 @@ +from sys import argv as args +from tp_fichier import * + + +def __main__(): + if len(args) == 2: + if args[1] == "tp_fichier": + __tp_fichier__() + else: + print("Usage: python3 main.py") + return + else: + print("Usage: python3 main.py") + return + + +def __tp_fichier__(): + nb_lignes = calculer_nb_lignes("lorem_ipsum.txt") + print(nb_lignes) + recopier_fichier("lorem_ipsum.txt", "lorem_ipsum_copie.txt") + inverser_fichier("lorem_ipsum.txt", "lorem_ipsum_inv.txt") + extraire_personnes_majeures("ages.csv", "ages_majeurs.csv") + extraire_notes_matiere("notes.csv", "notes_matiere.csv", "PHYSIQUE") + extraire_notes_matiere("notes.csv", "notes_matiere_maths.csv", "maths") + extraire_notes_matiere("notes.csv", "notes_matiere_informatique.csv", "INFO") + + +if __name__ == "__main__": + __main__() diff --git a/TP fichier/notes.csv b/TP fichier/notes.csv new file mode 100644 index 0000000..c302d0d --- /dev/null +++ b/TP fichier/notes.csv @@ -0,0 +1,7 @@ +ID ETUDIANT;MATHS;PHYSIQUE;INFO +984061;19;11;12 +570962;2;15;19 +957416;17;13;9 +234156;18;0;6 +521084;11;15;2 +671350;16;3;17 diff --git a/TP fichier/notes_matiere.csv b/TP fichier/notes_matiere.csv new file mode 100644 index 0000000..bddfdb5 --- /dev/null +++ b/TP fichier/notes_matiere.csv @@ -0,0 +1,7 @@ +ID ETUDIANT;PHYSIQUE +984061;11 +570962;15 +957416;13 +234156;0 +521084;15 +671350;3 diff --git a/TP fichier/notes_matiere_informatique.csv b/TP fichier/notes_matiere_informatique.csv new file mode 100644 index 0000000..36cf1d8 --- /dev/null +++ b/TP fichier/notes_matiere_informatique.csv @@ -0,0 +1,13 @@ +ID ETUDIANT;INFO +984061;12 + +570962;19 + +957416;9 + +234156;6 + +521084;2 + +671350;17 + diff --git a/TP fichier/notes_matiere_maths.csv b/TP fichier/notes_matiere_maths.csv new file mode 100644 index 0000000..af7db8c --- /dev/null +++ b/TP fichier/notes_matiere_maths.csv @@ -0,0 +1,7 @@ +ID ETUDIANT;MATHS +984061;19 +570962;2 +957416;17 +234156;18 +521084;11 +671350;16 diff --git a/TP fichier/tp_fichier.pdf b/TP fichier/tp_fichier.pdf new file mode 100644 index 0000000..24d61f0 Binary files /dev/null and b/TP fichier/tp_fichier.pdf differ diff --git a/TP fichier/tp_fichier.py b/TP fichier/tp_fichier.py new file mode 100644 index 0000000..114a089 --- /dev/null +++ b/TP fichier/tp_fichier.py @@ -0,0 +1,64 @@ +def calculer_nb_lignes(nom_fichier) : + file = open(nom_fichier, "r") + nb_lignes = 0 + for line in file: + nb_lignes += 1 + file.close() + return nb_lignes + + +def recopier_fichier(nom_fichier_source, nom_fichier_destination): + file_source = open(nom_fichier_source, "r") + file_destination = open(nom_fichier_destination, "w") + for line in file_source: + file_destination.write(line) + file_source.close() + file_destination.close() + + +def inverser_fichier(nom_fichier_source, nom_fichier_destination) : + file_source = open(nom_fichier_source, "r") + fichier_destination = open(nom_fichier_destination, "w") + lignes = file_source.readlines() + lignes.reverse() + for ligne in lignes: + fichier_destination.write(ligne) + file_source.close() + fichier_destination.close() + + +def extraire_personnes_majeures(nom_fichier_source, nom_fichier_destination): + file_source = open(nom_fichier_source, "r") + file_destination = open(nom_fichier_destination, "w") + lignes = file_source.readlines() + file_destination.write(lignes[0]) + for ligne in lignes[1:]: + age = int(ligne.split(";")[2]) + if age >= 18: + file_destination.write(ligne) + file_source.close() + file_destination.close() + + +def extraire_notes_matiere(nom_fichier_source, nom_fichier_destination, nom_matiere): + file_source = open(nom_fichier_source, "r") + file_destination = open(nom_fichier_destination, "w") + lignes = file_source.readlines() + matieres = lignes[0].split(";") + matieres[-1] = matieres[-1][:-1] + index_matiere = -1 + nom_matiere = nom_matiere.upper() + for i in range(len(matieres)): + if matieres[i] == nom_matiere: + index_matiere = i + break + if index_matiere == -1: + print("La matière n'a pas été trouvée") + return + file_destination.write(matieres[0] + ";" + nom_matiere + "\n") + for ligne in lignes[1:]: + print(ligne) + notes = ligne.split(";") + file_destination.write(notes[0] + ";" + notes[index_matiere] + "\n") + file_source.close() + file_destination.close() \ No newline at end of file