Bienvenue dans ce cours !

Introduction à l'importation de données en Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Importer des données

  • Fichiers plats, par exemple .txt, .csv
  • Fichiers provenant d'autres logiciels

 

ch_1_1_v2.004.png

Introduction à l'importation de données en Python

Importer des données

  • Fichiers plats, par exemple .txt, .csv
  • Fichiers provenant d'autres logiciels
  • Bases de données relationnelles

ch_1_1_v2.005.png

Introduction à l'importation de données en Python

Fichiers texte brut

ch_1_1_v2.007.png

Introduction à l'importation de données en Python

Données de la table

titanic.csv

                        Name      Sex  Cabin  Survived
     Braund, Mr. Owen Harris     male    NaN         0
  Cumings, Mrs. John Bradley   female    C85         1
      Heikkinen, Miss. Laina   female    NaN         1
Futrelle, Mrs. Jacques Heath   female   C123         1
    Allen, Mr. William Henry     male    NaN         0
1 Source : Kaggle
Introduction à l'importation de données en Python

Données de la table

titanic.csv

titanic.csv avec une ligne mise en évidence

Introduction à l'importation de données en Python

Données de la table

titanic.csv

titanic.csv avec une colonne mise en évidence

  • Fichier plat
Introduction à l'importation de données en Python

Lire un fichier texte

filename = 'huck_finn.txt'

file = open(filename, mode='r') # 'r' is to read
text = file.read()
file.close()
Introduction à l'importation de données en Python

Imprimer un fichier texte

print(text)
YOU don't know about me without you have read a book by
the name of The Adventures of Tom Sawyer; but that 
ain't no matter. That book was made by Mr. Mark Twain,
and he told the truth, mainly. There was things which 
he stretched, but mainly he told the truth.  That is 
nothing. never seen anybody but lied one time or 
another, without it was Aunt Polly, or the widow, or 
maybe Mary. Aunt Polly--Tom's Aunt Polly, she is--and
Mary, and the Widow Douglas is all told about in that
book, which is mostly a true book, with some 
stretchers, as I said before.
Introduction à l'importation de données en Python

Écrire dans un fichier

filename = 'huck_finn.txt'
file = open(filename, mode='w')  # 'w' is to write
file.close()
Introduction à l'importation de données en Python

Gestionnaire de contexte avec

with open('huck_finn.txt', 'r') as file:
    print(file.read())
YOU don't know about me without you have read a book by
the name of The Adventures of Tom Sawyer; but that 
ain't no matter. That book was made by Mr. Mark Twain,
and he told the truth, mainly. There was things which 
he stretched, but mainly he told the truth.  That is 
nothing. never seen anybody but lied one time or 
another, without it was Aunt Polly, or the widow, or 
maybe Mary. Aunt Polly--Tom's Aunt Polly, she is--and 
Mary, and the Widow Douglas is all told about in that 
book, which is mostly a true book, with some 
stretchers, as I said before.
Introduction à l'importation de données en Python

Dans les exercices, vous allez :

  • Imprimer les fichiers sur la console
  • Imprimer des lignes spécifiques
  • Discuter des fichiers plats
Introduction à l'importation de données en Python

Passons à la pratique !

Introduction à l'importation de données en Python

Preparing Video For Download...