mirror of
https://github.com/BreizhHardware/py_A2.git
synced 2026-01-18 16:47:38 +01:00
TP Fichier Début et Fin
This commit is contained in:
7
TP fichier/ages.csv
Normal file
7
TP fichier/ages.csv
Normal 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
|
||||
|
4
TP fichier/ages_majeurs.csv
Normal file
4
TP fichier/ages_majeurs.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
NOM;PRENOM;AGE
|
||||
Pierre;Durand;46
|
||||
Nathalie;Martin;35
|
||||
Nicolas;Petit;70
|
||||
|
4
TP fichier/lorem_ipsum.txt
Normal file
4
TP fichier/lorem_ipsum.txt
Normal 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.
|
||||
4
TP fichier/lorem_ipsum_copie.txt
Normal file
4
TP fichier/lorem_ipsum_copie.txt
Normal 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.
|
||||
4
TP fichier/lorem_ipsum_inv.txt
Normal file
4
TP fichier/lorem_ipsum_inv.txt
Normal 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
29
TP fichier/main.py
Normal 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
7
TP fichier/notes.csv
Normal 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
|
||||
|
7
TP fichier/notes_matiere.csv
Normal file
7
TP fichier/notes_matiere.csv
Normal file
@@ -0,0 +1,7 @@
|
||||
ID ETUDIANT;PHYSIQUE
|
||||
984061;11
|
||||
570962;15
|
||||
957416;13
|
||||
234156;0
|
||||
521084;15
|
||||
671350;3
|
||||
|
13
TP fichier/notes_matiere_informatique.csv
Normal file
13
TP fichier/notes_matiere_informatique.csv
Normal file
@@ -0,0 +1,13 @@
|
||||
ID ETUDIANT;INFO
|
||||
984061;12
|
||||
|
||||
570962;19
|
||||
|
||||
957416;9
|
||||
|
||||
234156;6
|
||||
|
||||
521084;2
|
||||
|
||||
671350;17
|
||||
|
||||
|
7
TP fichier/notes_matiere_maths.csv
Normal file
7
TP fichier/notes_matiere_maths.csv
Normal file
@@ -0,0 +1,7 @@
|
||||
ID ETUDIANT;MATHS
|
||||
984061;19
|
||||
570962;2
|
||||
957416;17
|
||||
234156;18
|
||||
521084;11
|
||||
671350;16
|
||||
|
BIN
TP fichier/tp_fichier.pdf
Normal file
BIN
TP fichier/tp_fichier.pdf
Normal file
Binary file not shown.
64
TP fichier/tp_fichier.py
Normal file
64
TP fichier/tp_fichier.py
Normal 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()
|
||||
Reference in New Issue
Block a user