{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Manipulation de fichiers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Problème : un fichier liste1s1.txt contient le prénom de 34 élèves (un prénom par ligne, 34 lignes, un saut de ligne à la fin de chaque ligne).\n", "Récupérons ces 34 prénoms pour former une liste de 34 chaînes de caractères." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ouverture d'un fichier en lecture et lecture du fichier" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sakina\n", "Lilou\n", "Corentin\n", "Gaia\n", "Lisa\n", "Gabrielle\n", "Imène\n", "Nicolas\n", "Matéo\n", "Naïm\n", "Axiane\n", "Mazarine\n", "Suzanne\n", "Youenn\n", "Félix\n", "Melissa\n", "Camille\n", "Lisy-Anne\n", "Hélène\n", "Noa\n", "Justine\n", "Salim\n", "Malo\n", "Margot\n", "Gwendal\n", "Chourouk\n", "Samuel\n", "Elliot\n", "Noé\n", "Ambre\n", "Antoine\n", "Cléome\n", "Hugo\n", "Alban\n" ] } ], "source": [ "monFichier = open(\"/media/pv/1B71-6E9B/lycee/20182019/1S/liste1s1.txt\", \"r\") #\"r\" pour read : le fichier est ouvert en lecture\n", "print(monFichier.read())\n", "monFichier.close() #fermeture du fichier" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Créons une liste contenant ces prénoms :" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Sakina\\n', 'Lilou\\n', 'Corentin\\n', 'Gaia\\n', 'Lisa\\n', 'Gabrielle\\n', 'Imène\\n', 'Nicolas\\n', 'Matéo\\n', 'Naïm\\n', 'Axiane\\n', 'Mazarine\\n', 'Suzanne\\n', 'Youenn\\n', 'Félix\\n', 'Melissa\\n', 'Camille\\n', 'Lisy-Anne\\n', 'Hélène\\n', 'Noa\\n', 'Justine\\n', 'Salim\\n', 'Malo\\n', 'Margot\\n', 'Gwendal\\n', 'Chourouk\\n', 'Samuel\\n', 'Elliot\\n', 'Noé\\n', 'Ambre\\n', 'Antoine\\n', 'Cléome\\n', 'Hugo\\n', 'Alban']\n" ] } ], "source": [ "monFichier = open(\"/media/pv/1B71-6E9B/lycee/20182019/1S/liste1s1.txt\", \"r\")\n", "liste1s1 = []\n", "for ligne in monFichier:\n", " liste1s1 = liste1s1 + [ligne]\n", "monFichier.close()\n", "print(liste1s1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On remarque les \\n (sauts de ligne) pas très jolis (sauf sur le dernier prénom).\n", "Supprimons les \\n dès le départ (NB : on ne peut pas modifier une chaîne de caractères ; il faut renommer line) : " ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Sakina', 'Lilou', 'Corentin', 'Gaia', 'Lisa', 'Gabrielle', 'Imène', 'Nicolas', 'Matéo', 'Naïm', 'Axiane', 'Mazarine', 'Suzanne', 'Youenn', 'Félix', 'Melissa', 'Camille', 'Lisy-Anne', 'Hélène', 'Noa', 'Justine', 'Salim', 'Malo', 'Margot', 'Gwendal', 'Chourouk', 'Samuel', 'Elliot', 'Noé', 'Ambre', 'Antoine', 'Cléome', 'Hugo', 'Alban']\n" ] } ], "source": [ "monFichier = open(\"/media/pv/1B71-6E9B/lycee/20182019/1S/liste1s1.txt\", \"r\")\n", "liste1s1 = []\n", "for ligne in monFichier:\n", " prenom= ligne.replace('\\n','')\n", " liste1s1 = liste1s1 + [prenom]\n", "monFichier.close()\n", "print(liste1s1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }