Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
icn:facultatif:c_langage_python_fichier [2019/09/26 15:17] techer.charles_educ-valadon-limoges.fr |
icn:facultatif:c_langage_python_fichier [2019/09/26 20:47] (Version actuelle) techer.charles_educ-valadon-limoges.fr |
||
---|---|---|---|
Ligne 68: | Ligne 68: | ||
La méthode readline permet de lire une seule ligne du fichier. | La méthode readline permet de lire une seule ligne du fichier. | ||
<code python> | <code python> | ||
- | mon_fichier = open(" | + | with open(" |
- | ligne = mon_fichier.readline() | + | |
</ | </ | ||
Ici, ligne contient tous les champs de l' | Ici, ligne contient tous les champs de l' | ||
<code python> | <code python> | ||
- | mon_fichier = open(" | + | with open(" |
- | ligne = mon_fichier.readline() | + | |
- | nom = ligne.split(';' | + | |
- | prenom = ligne.split(';' | + | |
+ | | ||
</ | </ | ||
ligne.split(' | ligne.split(' | ||
- | === Comment détecter la fin d' | + | Dans l'instruction **print** est utilisé |
- | Il faut savoir que chaque ligne se termine par un caractère de contrôle invisible \n qui correspond à " | + | === Comment |
- | Si l'on veut lire un fichier | + | |
- | <code python> | + | |
- | mon_fichier = open(" | + | |
- | ligne = mon_fichier.readline() | + | |
- | while ligne !=" | + | |
- | ligne = mon_fichier.readline() | + | |
- | # | + | |
- | mon_fichier.close | + | |
- | </ | + | |
- | **Solution plus élégante avec une boucle for** : | + | Il suffit d' |
<code python> | <code python> | ||
- | mon_fichier = open(" | + | with open(" |
- | for ligne in mon_fichier: | + | for ligne in mon_fichier: |
- | # | + | nom = ligne.split(';' |
- | mon_fichier.close | + | |
+ | print(f' | ||
</ | </ | ||
==== Ecriture dans un fichier ==== | ==== Ecriture dans un fichier ==== | ||
Ligne 107: | Ligne 100: | ||
<code python> | <code python> | ||
- | mon_fichier = open(" | + | with open(" |
- | mon_fichier.write(" | + | mon_fichier.write(" |
- | mon_fichier.close() | + | |
</ | </ | ||
<WRAP center round info> | <WRAP center round info> | ||
- | **Autre écriture** pour **ajouter** une information au fichier : | ||
- | <code python> | ||
- | with open(" | ||
- | | ||
- | </ | ||
le fichier est automatiquement fermé | le fichier est automatiquement fermé | ||
</ | </ | ||
Ligne 130: | Ligne 116: | ||
<code python> | <code python> | ||
import csv | import csv | ||
- | mon_fichier = open("eleves.csv", " | + | with open("test.csv", " |
- | contenu = csv.reader(mon_fichier, | + | contenu = csv.reader(mon_fichier, |
- | for ligne in contenu: | + | for ligne in contenu: |
- | # | + | |
- | print(ligne[1], " ", | + | |
- | mon_fichier.close | + | |
</ | </ | ||
- | === Ecrire dans une fichier CSV === | + | === Ecrire dans un fichier CSV === |
<code python> | <code python> | ||
import csv | import csv | ||
- | mon_fichier = open("eleves.csv", " | + | with open("test.csv", " |
- | contenu = csv.writer(mon_fichier) | + | contenu = csv.writer(mon_fichier, quotechar=';' |
- | contenu.writerow( (" | + | contenu.writerow( (" |
- | mon_fichier.close | + | |
</ | </ | ||
+ | |||
+ | * lors de l' | ||
+ | * le paramètre **quotechar=';' | ||
===== Gérer des dictionnaires ===== | ===== Gérer des dictionnaires ===== | ||
Python permet d' | Python permet d' |