TP Fichier Début et Fin

This commit is contained in:
2024-02-14 10:54:58 +01:00
parent bdccea10f8
commit 4bef1459aa
12 changed files with 150 additions and 0 deletions

7
TP fichier/ages.csv Normal file
View File

@@ -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
1 NOM PRENOM AGE
2 Marie Dupond 12
3 Pierre Durand 46
4 Nathalie Martin 35
5 Jean Thomas 17
6 Isabelle Dubois 9
7 Nicolas Petit 70

View File

@@ -0,0 +1,4 @@
NOM;PRENOM;AGE
Pierre;Durand;46
Nathalie;Martin;35
Nicolas;Petit;70
1 NOM PRENOM AGE
2 Pierre Durand 46
3 Nathalie Martin 35
4 Nicolas Petit 70

View File

@@ -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.

View File

@@ -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.

View File

@@ -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.

29
TP fichier/main.py Normal file
View File

@@ -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__()

7
TP fichier/notes.csv Normal file
View File

@@ -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
1 ID ETUDIANT MATHS PHYSIQUE INFO
2 984061 19 11 12
3 570962 2 15 19
4 957416 17 13 9
5 234156 18 0 6
6 521084 11 15 2
7 671350 16 3 17

View File

@@ -0,0 +1,7 @@
ID ETUDIANT;PHYSIQUE
984061;11
570962;15
957416;13
234156;0
521084;15
671350;3
1 ID ETUDIANT PHYSIQUE
2 984061 11
3 570962 15
4 957416 13
5 234156 0
6 521084 15
7 671350 3

View File

@@ -0,0 +1,13 @@
ID ETUDIANT;INFO
984061;12
570962;19
957416;9
234156;6
521084;2
671350;17
1 ID ETUDIANT INFO
2 984061 12
3 570962 19
4 957416 9
5 234156 6
6 521084 2
7 671350 17

View File

@@ -0,0 +1,7 @@
ID ETUDIANT;MATHS
984061;19
570962;2
957416;17
234156;18
521084;11
671350;16
1 ID ETUDIANT MATHS
2 984061 19
3 570962 2
4 957416 17
5 234156 18
6 521084 11
7 671350 16

BIN
TP fichier/tp_fichier.pdf Normal file

Binary file not shown.

64
TP fichier/tp_fichier.py Normal file
View File

@@ -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()