CentraleSupélecDépartement informatique
Plateau de Moulon
3 rue Joliot-Curie
F-91192 Gif-sur-Yvette cedex
CSV files

The code below is used to read from a CSV file having the following format:

  • The first line gives the name of the four columns
  • The other lines give the rows with values for each column given as value1;value2;value3;value4.
import csv

with open("file.csv", "r", encoding="utf-8") as csv_file:
      csv_reader = csv.reader(csv_file, delimiter=";")
      next(csv_reader)
      for row in csv_reader:
          print("The first value in the row is", row[0])
          print("The first value in the row is", row[1])
          print("The first value in the row is", row[2])
          print("The first value in the row is", row[3])

Here is the explanation of the code:

  • Line 1. Imports the package csv that provides the necessary functions to manipulate CSV files.
  • Line 3. Opens a CSV file in reading ("r") mode.
  • Line 4. Creates an object that allows reading the CSV file. We specify that the delimiter is the character ";".
  • Line 5. Skips one line of the CSV file. In this case, we skip the first line that contain the header.
  • Iterates through the rows of the CSV file. Each row is a list of values.